从现有图像裁剪图像 [英] Cropping out an image from an existing image

查看:224
本文介绍了从现有图像裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从现有图片裁剪图片。我已经拍摄了一个图像,并应用单色的阈值 98%使用imagemagick(这是在openCV吗?)



产生的图片如下:





现在从这个图像中,我想裁剪出另一个图像,使最终图像看起来像这样:



>



问题
我如何在OpenCV中执行此操作?注意,我想裁剪图片的唯一原因是,我可以使用:





在上面的输出中,您感兴趣的区域将是2nd 图片中的最大矩形



在旁注,如果您计划稍后隔离文本,可以使用简单的 cv :: erode() 可以删除该图片中的所有噪音,因此您可以使用白框&文本。另一种删除噪音的方法是使用 cv :: medianBlur () 。您也可以浏览 cv:syntaxEx()

  cv: :Mat kernel = cv :: getStructuringElement(cv :: MORPH_ELLIPSE,cv :: Size(7,7),cv :: Point(3,3)); 
cv :: morphologyEx(src,src,cv :: MORPH_ELLIPSE,kernel);

一个合适的解决方案甚至可以是这些的组合3.我展示了一点on 从X射线图像中提取手骨


I would like to crop out an image from an existing image. I've taken an image and applied monochrome on it with threshold 98% using imagemagick (is this doable in openCV?)

The resulting image is this:

Now from this Image I would like to crop out another image so that the final image looks like this:

Question How can I do this in OpenCV? Note, the only reason I want to crop the image is so that I can use this answer to get the part of the text. If there is no need to crop out a new image and instead just concentrate on black part of the image to begin with, that would be great.

解决方案

If the text at the top and at the bottom are the regions that you want to crop out, if they are always at the same location the solution is easy: just set a ROI that ignores those areas:

#include <cv.h>
#include <highgui.h>

int main(int argc, char* argv[])
{
    cv::Mat img = cv::imread(argv[1]);
    if (img.empty())
    {
        std::cout << "!!! imread() failed to open target image" << std::endl;
        return -1;        
    }

    /* Set Region of Interest */

    int offset_x = 129;
    int offset_y = 129;

    cv::Rect roi;
    roi.x = offset_x;
    roi.y = offset_y;
    roi.width = img.size().width - (offset_x*2);
    roi.height = img.size().height - (offset_y*2);

    /* Crop the original image to the defined ROI */

    cv::Mat crop = img(roi);
    cv::imshow("crop", crop);
    cv::waitKey(0);

    cv::imwrite("noises_cropped.png", crop);

    return 0;
}

Output image:

If the position of the black rectangle, which is your area of interest, is not present on a fixed location then you might want to check out another approach: use the rectangle detection technique:

On the output above, the area you are interested will be 2nd largest rectangle in the image.

On a side note, if you plan to isolate the text later, a simple cv::erode() could remove all the noises in that image so you are left with the white box & text. Another technique to remove noises is to use cv::medianBlur().You can also explore cv::morphologyEx() to do that trick:

cv::Mat kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(7, 7), cv::Point(3, 3));
cv::morphologyEx(src, src, cv::MORPH_ELLIPSE, kernel);    

A proper solution might even be a combination of these 3. I've demonstrated a little bit of that on Extract hand bones from X-ray image.

这篇关于从现有图像裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆