学习OpenCV——DrawRect

 
 

分别从x,y,h,w分别向中靠近,直到包含区域的像素平均值小于上一次的平均值。分别获取(x,y)(w,h)画框

对二值图像效果比较好

 
 
#include "cv.h"
#include "highgui.h"

int main()
{
    IplImage* src=NULL;
    IplImage* dst= NULL;
    src= cvLoadImage("D:/1.jpg",-1);//<0 代表相应颜色或深度
    
	
	int x=0,y=0,w=src->width,h=src->height;
	const int w0=src->width;
	const int h0=src->height;
	//cvRectangle(src,cvPoint(x,y),cvPoint(x+w,y+h),CV_RGB(0,0,255),2);
	//cvNamedWindow("src0",1);
    //cvShowImage("src0",src);

	double t0=cvAvg(src).val[0];
	double t1=t0;

	for(int i=1;i<w0;i++)
	{
		++x;
		cvSetImageROI(src,cvRect(x,0,w0,h0));
		
		double t2=0;
		t2=cvAvg(src).val[0];
		if(t2>t1)
			t1=t2;
		else
			break;
		cvResetImageROI(src);
	}
	cvResetImageROI(src);
	t1=t0;
	for(int i=1;i<w0;i++)
	{
		--w;
		cvSetImageROI(src,cvRect(0,0,w,h0));
		
		double t2=cvAvg(src).val[0];
		if(t2>t1)
			t1=t2;
		else
			break;
		cvResetImageROI(src);
	}
	cvResetImageROI(src);

	t1=t0;
	for(int i=1;i<h0;i++)
	{
		++y;
		cvSetImageROI(src,cvRect(0,y,w0,h0));
		
		double t2=0;
		t2=cvAvg(src).val[0];
		if(t2>t1)
			t1=t2;
		else
			break;
		cvResetImageROI(src);
	}
	cvResetImageROI(src);
	t1=t0;
	for(int i=1;i<h0;i++)
	{
		--h;
		cvSetImageROI(src,cvRect(0,0,w0,h));
		
		double t2=cvAvg(src).val[0];
		if(t2>=t1)
			t1=t2;
		else
			break;
		cvResetImageROI(src);
	}
	cvResetImageROI(src);
	
	

	cvResetImageROI(src);
	cvRectangle(src,cvPoint(x,y),cvPoint(w,h),CV_RGB(0,0,255),2);
	cvNamedWindow("src",1);
    cvShowImage("src",src);
	cvSaveImage("D:/out_2尺度FT+2尺度SR(MS&64、32)加权归一化AddWeighted(0.9,0.7)/0_1_1664_RC_Draw.png",src);
	cvWaitKey(0);
}
 


 

 

 

你可能感兴趣的:(null,DST)