2017-12-17 3 views
0

Es funktioniert, aber die Farbe meiner Kontur ist schwarz. Wie ändere ich es auf rot oder grün?Python Opencv drawContour Fehler

import numpy as np 
    import cv2 
    from matplotlib import pyplot as plt 
    img = cv2.imread('1.jpg',0) 
    img1 = cv2.imread('5.jpg',0) 
    dest = cv2.subtract(img, img1) 
    res = cv2.bitwise_not(dest) 
    ret , threshold = cv2.threshold(res,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)   
    cv2.namedWindow('thresimage', cv2.WINDOW_NORMAL) 
    cv2.imshow('thresimage',threshold) 
    _, contours, hierarchy = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 
    print "Number of contours detected %d -> "%len(contours) 
    cv2.drawContours(threshold,contours,-1,(0,255,0),3) 
    cv2.namedWindow('contour', cv2.WINDOW_NORMAL) 
    cv2.imshow('contour',threshold) 
    cv2.waitKey(0) 
    cv2.destroyAllWindows() 

enter image description here

Antwort

2

Erste:

contours, hierarchy, _ = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 

==>

_, contours, hierarchy = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 

Help on built-in function findContours: 

findContours(...) 
    findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy 
    . @brief Finds contours in a binary image. 
    . 
    . The function retrieves contours from the binary image using the algorithm @cite Suzuki85 . The contours 
    . are a useful tool for shape analysis and object detection and recognition. See squares.cpp in the 
    . OpenCV sample directory. 
    . @note Since opencv 3.2 source image is not modified by this function. 
    . 
    . @param image Source, an 8-bit single-channel image. Non-zero pixels are treated as 1's. Zero 
    . pixels remain 0's, so the image is treated as binary . You can use cv::compare, cv::inRange, cv::threshold , 
    . cv::adaptiveThreshold, cv::Canny, and others to create a binary image out of a grayscale or color one. 
    . If mode equals to cv::RETR_CCOMP or cv::RETR_FLOODFILL, the input can also be a 32-bit integer image of labels (CV_32SC1). 
    . @param contours Detected contours. Each contour is stored as a vector of points (e.g. 
    . std::vector<std::vector<cv::Point> >). 
    . @param hierarchy Optional output vector (e.g. std::vector<cv::Vec4i>), containing information about the image topology. It$ 
    . as many elements as the number of contours. For each i-th contour contours[i], the elements 
    . hierarchy[i][0] , hierarchy[i][1] , hierarchy[i][2] , and hierarchy[i][3] are set to 0-based indices 
    . in contours of the next and previous contours at the same hierarchical level, the first child 
    . contour and the parent contour, respectively. If for the contour i there are no next, previous, 
    . parent, or nested contours, the corresponding elements of hierarchy[i] will be negative. 
    . @param mode Contour retrieval mode, see cv::RetrievalModes 
    . @param method Contour approximation method, see cv::ContourApproximationModes 
    . @param offset Optional offset by which every contour point is shifted. This is useful if the 
    . contours are extracted from the image ROI and then they should be analyzed in the whole image 
    . context. 
Zweite

ret , threshold = cv2.threshold(res,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 
# ... 
cv2.drawContours(threshold,contours,-1,(0,255,0),3) 

Sie in Farbe zeichnen (0,255,0) auf binäre threshed Bild, dann wird es immer das erste Element sein 0, das ist schwarz. Sie sollten zuerst grau in BGR konvertieren und dann Farbe zeichnen.

canvas = cv2.cvtColor(threshold, cv2.COLOR_GRAY2BGR) 
cv2.drawContours(canvas,contours,-1,(0,255,0),3) 
+0

Vielen Dank! Es funktioniert, aber ich kann die Konturen nach dem Zeichnen nicht sehen. –

+0

@ BùiChíThanh Poste dein Ergebnis, sonst können wir es nicht diagnostizieren. – Silencer

+0

Ich bearbeite meine Frage. –