How to slice/cut an image into pieces

2019-04-01 23:56发布

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.

2条回答
Rolldiameter
2楼-- · 2019-04-01 23:58

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);
查看更多
一夜七次
3楼-- · 2019-04-02 00:15

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);
    }
}
查看更多
登录 后发表回答