机器视觉实验之手掌分割

机器视觉实验之图像分割


一、实验内容:

      1)采用不同的分割方法(查阅书籍,文献资料和专业论坛)对采集的掌静脉图像进行前景和背景的分离,获得二值化手形图像,示例见图2;

 

     


二、实验代码(C++)

       实验平台:window7  OpenCV  VS2010

// segment hand from a simple background
// Author : imoptimistic 
// Date   : 2014-10-29 
// HomePage : http://blog.csdn.net/u012062327 
// Email  : 1433252800@qq.com  

#include <opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

// Global Variables
Mat image,image_bi,mask;
const int threshold_diff_max=255;
      int threshold_value;
	  int area,minArea =1000;
	  

// @function on_segent();@brief Callback for segment 
void on_segment( int, void* )
{
   vector<vector<Point> > contours,fil_contours;
   vector<Vec4i>hierarchy;

   threshold(image,image_bi,threshold_value, 255, CV_THRESH_BINARY);
   imshow( "dst_bi",image_bi);

   //find the contours
   image_bi.copyTo(mask);
   findContours(mask, contours,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

   // Eliminate too samll area
   for(int i=0;i<contours.size();i++)  
   {  
	   area = fabs(contourArea(contours[i])); //获取当前轮廓面积 
	   
       if(area >minArea)  
      {  
		 
		  fil_contours.push_back(contours[i]);
	  }

	}
   mask.setTo(Scalar(0));
   drawContours(mask,fil_contours,
		-1, // draw all contours
		255,// in white
		0); // with a thickness of 0
   
   imshow("dst_con",mask);
}

int main(int argc, char** argv)
{
   image = imread("vl.bmp");
   cvNamedWindow("src",CV_WINDOW_AUTOSIZE);
   cvNamedWindow("dst_bi",CV_WINDOW_AUTOSIZE);
   cvNamedWindow("dst_con",CV_WINDOW_AUTOSIZE);
   
   threshold_value=35;
   GaussianBlur(image,image,Size(3,3),0,0);
   erode(image,image,NULL);
   dilate(image,image,NULL);
   cvtColor(image,image,CV_BGR2GRAY); 
   imshow("src",image);

   // Create Trackbars
   char TrackbarName[50];
   sprintf( TrackbarName, "threshold", threshold_diff_max );
   createTrackbar( TrackbarName, "dst_bi", &threshold_value, threshold_diff_max , on_segment);
   on_segment(threshold_value,0);

    ///Wait until user press some key
    cvWaitKey(0);
	return 0;
}


三、实验效果



                              原图

二值化图像

手掌边缘

可通过滑动滚条来改变二值化的阈值来获得不同的二值化图像。

 

posted on 2014-10-29 21:36  rongbohou  阅读(357)  评论(0编辑  收藏  举报