作物三角与OpenCV的C ++ [英] Crop Triangle with opencv c++

查看:276
本文介绍了作物三角与OpenCV的C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户,

输入图像的描述在这里

我要裁剪的三角图像上,并显示在OpenCV的C ++的另一扇窗。我知道所有的三个坐标。 谁能帮助我?我没有找到有关三角种植在互联网上的任何答复。谢谢!

编辑:这里的问题是,我不能用投资回报率裁剪三角。我只复制三角形没有任何背景什么的周围。是否有可能通过了解三角形的坐标来创建自己的投资回报率[P1(302179),P2(329178),P3(315205)]

解决方案

  CV ::垫inputImage的= CV :: imread(input.png);
如果(inputImage.channels()→1)
{
    CV :: cvtColor(inputImage的inputImage的CV_RGB2GRAY);
}

//你的实际坐标替换这些值
//我发现这些先救你提供的图像,然后
//使用Microsoft Paint
INT X0 = 242;
INT Y0 = 164;
INT X1 = 314;
INT Y1 = 38;
INT×2 = 387;
INT Y2 = 164;

//然后用这三个点创建一条线屏蔽
CV ::垫lineMask = CV ::垫::零(inputImage.size(),inputImage.type());
CV ::线(lineMask,CV ::点(X0,Y0),CV ::点(X1,Y1),CV ::标量(255,255,0),1,8,0);
CV ::线(lineMask,CV ::点(X0,Y0),CV ::点(X2,Y2),CV ::标量(255,255,0),1,8,0);
CV ::线(lineMask,CV ::点(X1,Y1),CV ::点(X2,Y2),CV ::标量(255,255,0),1,8,0);

//你的面具行执行轮廓检测
CV ::矢量< CV ::矢量< CV ::点>>轮廓;
CV ::矢量< CV :: Vec4i>层次结构;
CV :: findContours(lineMask,轮廓,层次结构,CV_RETR_TREE,CV_CHAIN​​_APPROX_SIMPLE,CV ::点(0,0));

//计算距离到轮廓
CV ::垫raw_dist(lineMask.size(),CV_32FC1);

的for(int i = 0; I< lineMask.rows;我++)
{
    对于(INT J = 0; J< lineMask.cols; J ++)
    {
        raw_dist.at&所述漂浮>(I,J)= CV :: pointPolygonTest(轮廓[0],CV :: Point2f(J,I),真);
    }
}

双MINVAL;双MAXVAL;
CV :: minMaxLoc(raw_dist,和放大器; MINVAL,和放大器; MAXVAL,0,0,CV ::垫());
MINVAL =的std :: ABS(MINVAL);
MAXVAL =的std :: ABS(MAXVAL);

//描绘的距离图形
CV ::垫面膜= CV ::垫::零(inputImage.size(),CV_8UC1);

的for(int i = 0; I< mask.rows;我++)
{
    对于(INT J = 0; J< mask.cols; J ++)
    {
        如果(raw_dist.at&所述漂浮>(I,J)℃的)
        {
            mask.at&其中; UCHAR>(I,J)=&的static_cast其中; UCHAR>(0);
            继续;
        }
        mask.at&其中; UCHAR>(I,J)=&的static_cast其中; UCHAR>(255);
    }
}

//逆输入图像
CV ::垫invInput;
CV :: bitwise_not(inputImage的invInput);

//然后让你的三角形只有区域
CV ::垫outputImage;
invInput.copyTo(outputImage,掩模);
CV :: bitwise_not(outputImage,outputImage);

//显示调试目的
CV :: imshow(inputImage的,inputImage的);
CV :: imshow(lineMask,lineMask);
CV :: imshow(面具,面具);
CV :: imshow(outputImage,outputImage);
CV :: waitKey();
 

这是你的 inputImage的

输入图像的描述在这里

这是你的 lineMask

输入图像的描述在这里

这是你创建的二进制面膜

输入图像的描述在这里

这是你的最后 outputImage

输入图像的描述在这里

参考文献:

OpenCV的画线

<一个href="http://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html"相对=nofollow> OpenCV的findContours

<一个href="http://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/point_polygon_test/point_polygon_test.html"相对=nofollow>点多边形测试

User,

I want to crop that Triangle on the image and show it in another window with opencv c++. I know all three Coordinates. Can anyone help me? I did not find any answer on the Internet about "triangle cropping". Thanks!

EDIT: The Problem here is that i cannot use ROI for cropping the Triangle. I have to copy just the triangle without any background or something around. Is it possible to create my own ROI by knowing the Coordinates of the triangle [p1(302,179), p2(329,178), p3(315,205)]?

解决方案

cv::Mat inputImage = cv::imread("input.png");
if (inputImage.channels() > 1)
{
    cv::cvtColor(inputImage, inputImage, CV_RGB2GRAY);
}

// replace these values with your actual coordinates
// I found these by first saving your provided image, then 
// using Microsoft Paint
int x0 = 242;
int y0 = 164;
int x1 = 314;
int y1 = 38;
int x2 = 387;
int y2 = 164;

// then create a line masking using these three points
cv::Mat lineMask = cv::Mat::zeros(inputImage.size(), inputImage.type());
cv::line(lineMask, cv::Point(x0, y0), cv::Point(x1, y1), cv::Scalar(255, 255, 0), 1, 8, 0);
cv::line(lineMask, cv::Point(x0, y0), cv::Point(x2, y2), cv::Scalar(255, 255, 0), 1, 8, 0);
cv::line(lineMask, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(255, 255, 0), 1, 8, 0);

// perform contour detection on your line mask
cv::vector<cv::vector<cv::Point>> contours;
cv::vector<cv::Vec4i> hierarchy;
cv::findContours(lineMask, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

// calculate the distance to the contour
cv::Mat raw_dist(lineMask.size(), CV_32FC1);

for (int i = 0; i < lineMask.rows; i++)
{
    for (int j = 0; j < lineMask.cols; j++)
    {
        raw_dist.at<float>(i, j) = cv::pointPolygonTest(contours[0], cv::Point2f(j, i), true);
    }
}

double minVal; double maxVal;
cv::minMaxLoc(raw_dist, &minVal, &maxVal, 0, 0, cv::Mat());
minVal = std::abs(minVal);
maxVal = std::abs(maxVal);

// depicting the distances graphically
cv::Mat mask = cv::Mat::zeros(inputImage.size(), CV_8UC1);

for (int i = 0; i < mask.rows; i++)
{
    for (int j = 0; j < mask.cols; j++)
    {
        if (raw_dist.at<float>(i, j) < 0)
        {
            mask.at<uchar>(i, j) = static_cast<uchar>(0);
            continue;
        }           
        mask.at<uchar>(i, j) = static_cast<uchar>(255);
    }
}

// inverse the input image
cv::Mat invInput;   
cv::bitwise_not(inputImage, invInput);

// then get only the region of your triangle
cv::Mat outputImage;
invInput.copyTo(outputImage, mask);
cv::bitwise_not(outputImage, outputImage);

// display for debugging purpose
cv::imshow("inputImage", inputImage);
cv::imshow("lineMask", lineMask);
cv::imshow("mask", mask);
cv::imshow("outputImage", outputImage);
cv::waitKey();  

This is your inputImage:

This is your lineMask:

This is your created binary mask:

And this is your final outputImage:

References:

OpenCV draw line

OpenCV findContours

Point Polygon Test

这篇关于作物三角与OpenCV的C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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