c++opencvの基本操作について

6517 ワード

画像を読み取る
#include  //   
using namespace cv; //  cv    

int main()
{
	// 【1】      
	Mat img = imread("D://ly_yun//images//GF1a.tif");
	// 【2】           
	imshow("【     】", img);
	// 【3】  6000 ms       
	waitKey(6000);
}

えいぞうふしょく
//---------------------------------------------------------------------------------------------- 
#include 
#include 

//-----------------------------------【        】---------------------------------------
//		  :            
//-----------------------------------------------------------------------------------------------  
using namespace cv;

//-----------------------------------【main( )  】--------------------------------------------
//		  :            ,          
//-----------------------------------------------------------------------------------------------
int main()
{
	//      
	Mat srcImage = imread("1.jpg");
	//    
	imshow("【  】    ", srcImage);
	//       
	Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
	Mat dstImage;
	erode(srcImage, dstImage, element);
	//      
	imshow("【   】    ", dstImage);
	waitKey(0);

	return 0;
}

グレースケール
#include 
#include
using namespace cv;

//-----------------------------------【main( )  】--------------------------------------------
//		  :            ,          
//-------------------------------------------------------------------------------------------------
int main()
{
	//【0】       
	Mat srcImage = imread("1.jpg");  //            1.jpg    
	imshow("【   】Canny    ", srcImage); 	//      
	Mat dstImage, edge, grayImage;	//    

	//【1】   src         (dst)
	dstImage.create(srcImage.size(), srcImage.type());

	//【2】           
	cvtColor(srcImage, grayImage, CV_BGR2GRAY);

	//【3】     3x3     
	blur(grayImage, edge, Size(3, 3));

	//【4】  Canny  
	Canny(edge, edge, 3, 9, 3);

	//【5】      
	imshow("【   】Canny    ", edge);

	waitKey(0);

	return 0;
}

//-------------------------------------------------------------------------start threshold(src Img,result,1,255,CV_THRESH_BINARY);namedWindow(「二値化された画像1」);imshow(「二値化後の画像1」,result);
void RegionGrowing(Mat srcImg, Mat& dstImg, Point pt, int thre)
{
	Point ptGrowing;//       
	int nGrowLabel = 0;//     
	int startPtValue = 0;//        
	int currPtValue = 0;//        

	//8  
	int mDir[8][2] = { { -1, -1 }, { 0, -1 }, { 1, -1 }, { -1, 0 }, { 1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 } };

	vector growPtVec;//     
	growPtVec.push_back(pt);//          

	Mat markImg = Mat::zeros(srcImg.size(), CV_8UC1);//   
	unsigned char *pData = (unsigned char *)(markImg.data + pt.y*markImg.step);
	pData[pt.x] = 255;//       

	startPtValue = ((unsigned char*)(srcImg.data + pt.y*srcImg.step))[pt.x];

	while (!growPtVec.empty())
	{
		Point currPt = growPtVec.back();//    vector      
		growPtVec.pop_back();//         

		for (int i = 0; i<8; i++)
		{
			ptGrowing.x = currPt.x + mDir[i][0];
			ptGrowing.y = currPt.y + mDir[i][1];
			//        
			if (ptGrowing.x<0 || ptGrowing.y<0 || (ptGrowing.x>srcImg.cols - 1) || (ptGrowing.y>srcImg.rows))
				continue;//         
			//        
			nGrowLabel = ((unsigned char*)(markImg.data + ptGrowing.y*markImg.step))[ptGrowing.x];
			if (nGrowLabel == 0)//     
			{
				currPtValue = ((unsigned char*)(srcImg.data + ptGrowing.y*srcImg.step))[ptGrowing.x];
				if (abs(currPtValue - startPtValue) <= thre)
				{
					((unsigned char*)(markImg.data + ptGrowing.y*markImg.step))[ptGrowing.x] = 255;
					growPtVec.push_back(ptGrowing);
				}
			}

		}

	}
	markImg.copyTo(dstImg);
}
int main()
{
	Mat srcImg = imread("D:\\ly_yun\\images\\LBP\\LPB.tif", 0);
	Mat result;
	if (srcImg.empty())
		printf("image read error");
//	----------------------------------     -------------------start
	threshold(srcImg, result, 1, 255, CV_THRESH_BINARY);
	namedWindow("       1");
	imshow("       1", result);
//	----------------------------------     -------------------end

	//----------------------------------     -------------------start
	namedWindow("[  ]");
	imshow("[  ]", srcImg);
	//      
	Mat element = getStructuringElement(MORPH_RECT, Size(10,10));
	Mat dstImage1;
	Mat dstImage2;
	Mat dstImage3;
	Mat dstImage;

	//       
	erode(result, dstImage1, element);
	namedWindow("    [   ]");
	imshow("    [   ]", dstImage1); //      ,             

	//       ,       
	morphologyEx(result, dstImage2, MORPH_OPEN, element);
	namedWindow("     [   ]");
	imshow("     [   ]", dstImage2);


	// dstImage2  ,      
	dilate(result, dstImage3, element);
	namedWindow("    [   ]");
	imshow("    [   ]", dstImage3);

//	 dstImage3         ,
	morphologyEx(result, dstImage, MORPH_CLOSE, element);
	namedWindow("     [   ]");
	imshow("     [   ]", dstImage);
	imwrite("D:\\ly_yun\\images\\LBP\\LPBmorphologyEx.tif", dstImage);
	Mat srcImg0 = imread("D:\\ly_yun\\images\\LBP\\LPBmorphologyEx.tif", 0);
	// dstImage2  ,      
	dilate(srcImg0, dstImage3, element);
	namedWindow("    [   ]");
	imshow("    [   ]", dstImage3);
	imwrite("D:\\ly_yun\\images\\LBP\\LPBdilate.tif", dstImage3);


	//                  ????????????????????????????????????????????????????
	erode(srcImg0, dstImage1, element);
	namedWindow("    [   ]");
	imshow("    [   ]", dstImage1); //      ,                
	imwrite("D:\\ly_yun\\images\\LBP\\LPBerode.tif", dstImage1);
	//----------------------------------     -------------------end
	Mat junzhidst; 
	blur(result, junzhidst, Size(15, 15), Point(-1, -1));//      
	namedWindow("    [   ]");
	imshow("    [   ]", dstImage);
	Mat gblur;
	//      
	GaussianBlur(result, gblur, Size(15, 15), 11, 11);//    
		imshow("    ", gblur);
	//----------------------------------     -------------------start
	Mat srcImg1 = result.clone();
	Mat outImg1, outImg2;
	RegionGrowing(srcImg1, outImg1, Point(241, 258), 10);
	RegionGrowing(srcImg1, outImg2, Point(302, 118), 80);
	add(outImg1, outImg2, outImg1);
	imshow("p1p2", outImg1);
	Mat resultImg;
	result.copyTo(resultImg, ~outImg1);
	imshow("outImg", resultImg);
	//----------------------------------     -------------------end
	waitKey(0);
	return 0;
}