How to slice/cut an image into pieces

人盡茶涼 提交于 2019-12-05 05:35:50

问题


So I have a function that receives OpenCV image and turns it into gray-scale.

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

How now to cut that image into images of size 10x10 pixels and iterate true them? (width and height may not divide on 10 but if there would be some loss (like loss from 1*h to 9*h+9*h pixels per image) it would be OK for me. ) BTW can you output one of 10*10 images onto screen. please.


回答1:


You can crop your images into small pieces like this (iteration not tested):

// 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);
    }
}



回答2:


I think the simplest solution is to use Regions of Interest. Here is sample

    /* 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);


来源:https://stackoverflow.com/questions/4875372/how-to-slice-cut-an-image-into-pieces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!