如何将图像切片/切成碎片

所以我有一个函数接收OpenCV图像并将其转换为灰度图像。

void UseLSD(IplImage* destination) { IplImage *destinationForGS = cvCreateImage(cvSize(destination->width, destination->height),IPL_DEPTH_8U,1); cvCvtColor(destination,destinationForGS,CV_RGB2GRAY); } 

现在如何将图像切割成大小为10×10像素的图像并迭代真实? (宽度和高度可能不会在10上划分,但是如果会有一些损失(例如从每个图像1 * h到9 * h + 9 * h像素的损失),对我来说就没问题了。)BTW可以输出10个中的一个* 10张图像到屏幕上。 请。

您可以将图像裁剪成这样的小块(迭代未经测试):

 // source image IplImage *source = cvLoadImage("lena.jpg", 1); int roiSize = 10; for(int j = 0; j < source->width/roiSize; ++j) { for(int i = 0; i < source->height/roiSize; ++i) { cvSetImageROI(source, cvRect(i*roiSize, j*roiSize, roiSize, roiSize)); // cropped image IplImage *cropSource = cvCreateImage(cvGetSize(source), source->depth, source->nChannels); // copy cvCopy(source, cropSource, NULL); // ... do what you want with your cropped image ... // always reset the ROI cvResetImageROI(source); } } 

我认为最简单的解决方案是使用感兴趣的区域。 这是样本

  /* load image */ IplImage *img1 = cvLoadImage("elvita.jpg", 1); /* sets the Region of Interest Note that the rectangle area has to be __INSIDE__ the image You just iterate througt x and y. */ cvSetImageROI(img1, cvRect(x*10, y*10, x*10 + 10, y*10 + 10)); /* create destination image Note that cvGetSize will return the width and the height of ROI */ IplImage *img2 = cvCreateImage(cvGetSize(img1), img1->depth, img1->nChannels); /* copy subimage */ cvCopy(img1, img2, NULL); /* always reset the Region of Interest */ cvResetImageROI(img1);