2011-02-02 154 views
2

因此,我有一個接收OpenCV圖像並將其轉換爲灰度圖的功能。如何將圖像分片/切片

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

現在該如何將該圖像剪切成10x10像素大小的圖像並迭代它們? (寬度和高度可能不會在10分割,但如果會有一些損失(如從1 * h到9 * h + 9 * h像素每張圖像的損失),這對我來說可以。) 順便說一句,你可以輸出一個的10 * 10圖像到屏幕上。請。

回答

4

您可以裁剪圖片切成小塊像這樣(迭代未測試):

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

OpenCV能否在大圖像上死? – Rella 2011-02-02 14:44:01

4

我認爲最簡單的解決方案是使用感興趣區域。這裏是樣本

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