2016-01-23 307 views
3

这里是我OpenCV:如何找到轮廓/多边形内的颜色?

im = cv2.imread('luffy.jpg') 
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 
ret,thresh = cv2.threshold(gray,127,255,0) 

contours,h = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 

for cnt in contours: 

    // return color inside of the contour here 
    mask = np.zeros(cnt.shape[:2],np.uint8) 
    mean = cv2.mean(cant,mask) // I think this is promising but so far it returns arrays with just zeros. I think its because I used np.zeros above to find the mask.... 
    moment = cv2.moments(cnt) //maybe this will help? 

我能找到没有这样的OpenCV的功能内置的。我想也许你可以用瞬间办呢?我怎样才能做到这一点?

编辑:由ZAW林给我的这个输入图像所提出的解决方案:

enter image description here

这个输出图像:

enter image description here

+0

一种方法是:您可以获取轮廓内的图像区域,然后将其用于进一步处理。裁剪内部区域看到这个:http://stackoverflow.com/questions/28759253/how-to-crop-the-internal-area-of-a-contour – Vipul

+0

我认为最好的方法是处理内部的直方图图片。 [本](http://www.pyimagesearch.com/2014/01/22/clever-girl-a-guide-to-utilizing-color-histograms-for-computer-vision-and-image-search-engines/ )可能会有帮助。 – Mahm00d

+0

第一个链接似乎给了一个空白的白色裁剪,所以我不能用它来找到颜色。直方图可能工作,但它似乎适合做一个实际的直方图。例如,我无法找到平均每个通道值的方法。我发现你可以在轮廓上运行cv2.mean(cnt,mask)来获得BGR通道的平均值,这看起来很有前途。到目前为止,虽然 – BigBoy1337

回答

4

这得到各轮廓内部的平均颜色,并绘制轮廓与该颜色最终图像。

import cv2 
import numpy as np 
im = cv2.imread('/home/zawlin/test.png') 

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 
contours,h = cv2.findContours(gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 

final = np.zeros(im.shape,np.uint8) 
mask = np.zeros(gray.shape,np.uint8) 

for i in xrange(0,len(contours)): 
    mask[...]=0 
    cv2.drawContours(mask,contours,i,255,-1) 
    cv2.drawContours(final,contours,i,cv2.mean(im,mask),-1) 

cv2.imshow('im',im) 
cv2.imshow('final',final) 
cv2.waitKey(0) 
+0

没有成功,我认为这是有效的,因为没有任何错误,但是它做了一件可怕的工作。你用它得到好的结果了吗? - 相同的图像基本上用平均颜色输出... – BigBoy1337

+0

我没有测试结果是否好。我只是使用了张贴的图片@sturkmen,我和他的结果一样。也许你可以发布你的图片? –

+0

我在问题中给出了一个示例。显然它的工作还不是很好,尽管我不确定原因是什么 – BigBoy1337

2

我觉得function mean带着面具图片是在轮廓中获取颜色的唯一方法,但对不起,我无法通过Python代码显示它。

您可以通过boundingRect得到边界轮廓的框,并用它来从源图像和二值图像获取图像ROI掩蔽(注意克隆二值图像的,因为findcontour破坏它)

也许一个C++会有用

#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include <iostream> 

using namespace cv; 
using namespace std; 

int main(int, char** argv) 
{ 
    /// Load source image 
    Mat src = imread(argv[1]); 
    if (src.empty()) 
    { 
    cerr << "No image supplied ..." << endl; 
    return -1; 
    } 

    /// Convert image to gray 
    Mat src_gray; 
    cvtColor(src, src_gray, COLOR_BGR2GRAY); 
    threshold(src_gray, src_gray, 50, 255, THRESH_BINARY); 
    imshow("src_gray", src_gray); 
    /// Find contours 
    vector<vector<Point> > contours; 
    findContours(src_gray.clone(), contours, RETR_TREE, CHAIN_APPROX_SIMPLE); 

    Mat resImage0 = src.clone(); 
    Mat resImage1 = src.clone(); 
    /// Draw contours 
    for(size_t i = 0; i< contours.size(); i++) 
    { 
     Scalar color = Scalar(0, 0, 255); 
     Rect _boundingRect = boundingRect(contours[i]); 
     Scalar mean_color0 = mean(src(_boundingRect)); 
     Scalar mean_color1 = mean(src(_boundingRect), src_gray(_boundingRect)); 

     drawContours(resImage0, contours, (int)i, mean_color0, FILLED); 
     drawContours(resImage1, contours, (int)i, mean_color1, FILLED); 
    } 

    /// Show in a window 
    imshow("src", src); 
    imshow("resImage0", resImage0); 
    imshow("resImage1", resImage1); 
    waitKey(0); 
    return(0); 
} 

输入图像(我的英文不好对不起。):

enter image description here

输出图像:

enter image description here enter image description here