Opencv 裁剪带有ConvexHull和ConvexDefect的图像
我使用下面的代码检测手,并在其中绘制了一个凸面外壳 下面是我的代码流程: 1) 角点检测(阈值) 2) 腐蚀+膨胀 3) 轮廓检测 4) 最大轮廓的凸面外壳 5) 凸性 6) Countour+船体绘制轮廓Opencv 裁剪带有ConvexHull和ConvexDefect的图像,opencv,image-processing,machine-learning,computer-vision,Opencv,Image Processing,Machine Learning,Computer Vision,我使用下面的代码检测手,并在其中绘制了一个凸面外壳 下面是我的代码流程: 1) 角点检测(阈值) 2) 腐蚀+膨胀 3) 轮廓检测 4) 最大轮廓的凸面外壳 5) 凸性 6) Countour+船体绘制轮廓 #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <stdio.h> #include <
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 147;
int max_thresh = 255;
RNG rng(12345);
/// Function header
void thresh_callback(int, void* );
/** @function main */
int main( int argc, char** argv )
{
// src = imread( "D:\\Projects\\Proposals\\Knuckle_Detection\\images\\picture028.jpg", 1 );
VideoCapture cap(0);
while(1)
{
cap>>src;
/// Convert image to gray and blur it
resize(src,src,Size(640,480),0,0,INTER_LINEAR);
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
/// Create Window
char* source_window = "Knuckle Extractor";
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
imshow( source_window, src );
thresh_callback( 0, 0 );
waitKey(5);
}
return(0);
}
/** @function thresh_callback */
void thresh_callback(int, void* )
{
Mat src_copy = src.clone();
Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Detect edges using Threshold
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY||CV_THRESH_OTSU );
imshow("b/f threshold", threshold_output);
erode(threshold_output,threshold_output,Mat ());
dilate(threshold_output,threshold_output,Mat ());
imshow("Threshold",threshold_output);
findContours( threshold_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
/// Find the convex hull object for each contour
vector<vector<Point > >hull( contours.size() );
vector<vector<Vec4i> >defects( contours.size() );
vector<vector<int > >hull1( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{
convexHull( Mat(contours[i]), hull[i], false );
convexHull( Mat(contours[i]), hull1[i], false );
}
for( int i = 0; i < contours.size(); i++ )
{
//convexHull( Mat(contours[i]), hull[i], false );
if (contours[i].size() >3 )
{
convexityDefects(contours[i], hull1[i], defects[i]);
cout<<"inside"<<endl;
}
}
/// Draw contours + hull results
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(10, 10), rng.uniform(0,255), rng.uniform(0,10) );
drawContours( src, contours, i, color, 5, 8, vector<Vec4i>(), 0, Point() );
drawContours( src, hull, i, color, 5, 8, vector<Vec4i>(), 0, Point() );
cout<<"in"<<endl;
cout<<"out"<<endl;
}
/// Show in a window
namedWindow( "Result", CV_WINDOW_AUTOSIZE );
imshow( "Result", src );
}
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
Mat-src;Mat src_gray;
int thresh=147;
int max_thresh=255;
RNG RNG(12345);
///函数头
无效阈值回调(int,void*);
/**@主功能*/
int main(int argc,字符**argv)
{
//src=imread(“D:\\Projects\\propositions\\Knuckle\u Detection\\images\\picture028.jpg”,1);
视频捕获上限(0);
而(1)
{
cap>>src;
///将图像转换为灰色并使其模糊
调整大小(src,src,大小(640480),0,0,内部线性);
CVT颜色(src、src_灰色、CV_BGR2灰色);
模糊(src_灰,src_灰,大小(3,3));
///创建窗口
char*source\u window=“指节提取器”;
namedWindow(源窗口、CV窗口、自动调整大小);
imshow(源窗口,src);
thresh_回调(0,0);
等待键(5);
}
返回(0);
}
/**@function thresh\u回调*/
无效阈值回调(int,void*)
{
Mat src_copy=src.clone();
Mat阈值输出;
矢量等值线;
向量层次;
///使用阈值检测边缘
阈值(src_灰度,阈值_输出,阈值,255,阈值|二进制| CV_阈值_大津);
imshow(“b/f阈值”,阈值输出);
腐蚀(阈值输出,阈值输出,Mat());
放大(阈值输出,阈值输出,Mat());
imshow(“阈值”,阈值输出);
findContours(阈值输出、轮廓、层次结构、CV_RETR_外部、CV_链近似_简单、点(0,0));
///查找每个轮廓的凸面外壳对象
向量壳(contours.size());
矢量缺陷(courtous.size());
vectorhull1(contours.size());
对于(int i=0;i3)
{
凸性缺陷(轮廓[i],hull1[i],缺陷[i]);
你能上传原始图像以便人们可以尝试进行一些图像处理吗?从目前的讨论来看,凸面外壳似乎不起作用。实际上,指关节似乎是预期的目标。如果是这样,我建议尝试使用纹理特定的过滤器来识别它们,然后继续从那里开始。