#include
#include
using namespace std;
int main()
{
IplImage *img = cvLoadImage("1.jpg");
IplImage *sub_image1 = nullptr,*sub_image2 = nullptr,*sub_image3 = nullptr;
CvRect rc1 = cvRect(0,0,img->width/3,img->height);
CvRect rc2 = cvRect(img->width/3,0,img->width/3,img->height);
CvRect rc3 = cvRect(img->width*2/3,0,img->width/3,img->height);
sub_image1 = cvCreateImage(cvSize( rc1.width, rc1.height),IPL_DEPTH_8U,3);
sub_image2 = cvCreateImage(cvSize( rc2.width, rc2.height),IPL_DEPTH_8U,3);
sub_image3 = cvCreateImage(cvSize( rc3.width, rc3.height),IPL_DEPTH_8U,3);
cout << "rc1.width = " << rc1.width << endl;
cout << "rc2.width = " << rc2.width << endl;
cout << "rc3.width = " << rc3.width << endl;
cout << "sub_image1->width = " << sub_image1 -> width << endl;
cout << "sub_image2->width = " << sub_image2 -> width << endl;
cout << "sub_image3->width = " << sub_image3 -> width << endl;
cout << img->width << endl;
cout << img->height<< endl;
cvDrawRect(img,cvPoint(rc1.x,rc1.y),cvPoint(rc1.x+rc1.width,rc1.y + rc1.height),cvScalar(255,255,0),8,0);
cvDrawRect(img,cvPoint(rc2.x,rc2.y),cvPoint(rc2.x+rc2.width,rc2.y + rc2.height),cvScalar(255,0,255),8,0);
cvDrawRect(img,cvPoint(rc3.x,rc3.y),cvPoint(rc3.x+rc3.width,rc3.y + rc3.height),cvScalar(0,255,255),8,0);
cvSetImageROI(img,rc1);
cvCopy(img,sub_image1);
cvSetImageROI(img,rc2);
cvCopy(img,sub_image2);
cvSetImageROI(img,rc3);
cvCopy(img,sub_image3);
cvResetImageROI(img);
cvNamedWindow("src",0);
cvShowImage("src",img);
cvNamedWindow("sub1",0);
cvShowImage("sub1",sub_image1);
cvNamedWindow("sub2",0);
cvShowImage("sub2",sub_image2);
cvNamedWindow("sub3",0);
cvShowImage("sub3",sub_image3);
cvWaitKey(0);
system("pause");
return 0;
}
在之前的项目中遇到过将图像三等分的问题,因此,可以通过设置ROI的方法将图像三等分,以上为将图像三等分的代码。在等分图像的时候,一定要记住:
CvRect 结构体的参数:x : CvRect 的左上角横坐标,y:CvRect的右上角纵坐标;
width:CvRect的宽度,hight:CvRect的高度;
这是与CRect等普通结构体不同的地方,CvRect的左下角坐标为(rect.x+rect.width,rect.y + rect.height)