java将图片均匀分成9份,OpenCV通过设置ROI将图像平均分成N等分

#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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值