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

发布于 2024-10-15 05:19:28 字数 428 浏览 5 评论 0原文

所以我有一个接收 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 图像之一输出到屏幕上。请。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2024-10-22 05:19:29

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

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

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);
    }
}
一念一轮回 2024-10-22 05:19:29

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文