2016-01-23 302 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