使用OpenCV将图像的区域移动到中心 [英] Move area of an image to the center using OpenCV

查看:518
本文介绍了使用OpenCV将图像的区域移动到中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将图像的一个区域移动到中心,我成功地获得了它的轮廓,我知道如何将它放在中心。
但是我想要的是移动轮廓内部的像素(黄色用黑色)在中心,而不只是轮廓(粉红色的 CV_FILLED )。

I'm trying to move a region of an image to the center, I succeeded in getting its contour and I know how to place this in the center. But what I want is to move the pixels that are inside the contour (yellow with black) at the center and not just the contour (which is pink by CV_FILLED).

图片:

代码:

//Then segment the image. save in  Mat crop
// ---- Center image -----
// pos :   contour interest
RotatedRect rr = fitEllipse(contours[pos]);
vector<Point>&contour  = contours[pos];
//http://stackoverflow.com/a/29467236/4595387
//difference between the centre of the image and centre of the contour
Point center = Point( crop.cols/2, crop.rows/2 );
int nX = center.x - rr.center.x;
int nY = center.y - rr.center.y;
for (size_t i=0; i< contour.size(); i++)
    {
        contour[i].x += nX;
        contour[i].y += nY;
    }
cout << "x: " << rr.center.x;
cout << "y: " << rr.center.y;
//color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0,    255));
//contour of the image to the center.
cv::drawContours(crop, contours, pos, color, CV_FILLED);

imshow("In",imagen_src);
imshow("Out",crop);


推荐答案

c> copyTo 。步骤在代码中注释。如果您需要不同的背景颜色,只需在下面的代码中更改 backgroundColor

You need basically to play around with copyTo with a mask. The steps are commented in the code. If you need a different background color, just change backgroundColor in the code below.

代码:

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{
    // Read image
    Mat3b img = imread("path_to_image");

    // Convert to hsv
    Mat3b hsv;
    cvtColor(img, hsv, COLOR_BGR2HSV);

    // Threshold on yellow color (in hsv space)
    Mat1b maskOnYellow;
    inRange(hsv, Scalar(20, 100, 100), Scalar(40, 255, 255), maskOnYellow);

    // Find contours of yellow item
    vector<vector<Point>> contours;
    findContours(maskOnYellow.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

    // Create a mask as a filled contour
    Mat1b mask(img.rows, img.cols, uchar(0));
    drawContours(mask, contours, 0, Scalar(255), CV_FILLED);

    // Get the bounding box of the item
    Rect box = boundingRect(contours[0]);

    // Get the roi in the input image according to the mask
    Mat3b item(img(box));

    // Create a black image (same size as the yellow item and same background bolor as result image) 
    // to copy the result of the segmentation
    Vec3b backgroundColor(0,0,0); // black
    Mat3b segmentedItem(item.rows, item.cols, backgroundColor);

    // Copy only the masked part
    item.copyTo(segmentedItem, mask(box));

    // Compute the center of the image
    Point center(img.cols / 2, img.rows / 2);

    // Create a result image
    Mat3b res(img.rows, img.cols, backgroundColor);

    // Compute the rectangle centered in the image, same size as box
    Rect centerBox(center.x - box.width/2, center.y - box.height/2, box.width, box.height);

    // Put the segmented item in the center of the result image
    segmentedItem.copyTo(res(centerBox));

    imshow("Result", res);
    waitKey();

    return 0;
}

输入:

结果:

这篇关于使用OpenCV将图像的区域移动到中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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