Можно ли раскрасить область c в контуре рисования в python? - PullRequest
2 голосов
/ 20 июня 2020

Я работаю над обработкой изображений в python, я обнаруживаю пятна с помощью cv2.minAreaRect и рисую повернутый прямоугольник, я этого добился. enter image description here

Now I want to fill the detected patch fully in white color, which means the area inside the cv2.drawContour in cyan color inside fully in white color(desire output has been done in ms-paint for reference) введите описание изображения здесь Я хочу добиться этого в python, возможно ли это в OpenCV- python?

Ответы [ 2 ]

4 голосов
/ 20 июня 2020

Допустим, у вас сохранены контуры в cnts. Затем следующий фрагмент заполнит повернутые прямоугольники голубым цветом.

import numpy as np
import cv2

for c in cnts:
    rotrect = cv2.minAreaRect(c)
    box = cv2.boxPoints(rotrect)
    box = np.int0(box)
    cv2.drawContours(image, [box], 0, (255, 255, 0), -1) # as opencv stores in BGR format
1 голос
/ 22 июня 2020

@ amras В соответствии с приведенными выше рекомендациями я изменил и опубликовал код, который я использовал для всех ваших ссылок

import cv2
import numpy as np
import matplotlib.pyplot as plt


image=cv2.imread("CP150036_001bw.png",0)
im2=cv2.imread("CP150036_001.png")
# convert to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# create a binary thresholded image
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
# show it
plt.imshow(binary, cmap="gray")
plt.show()
# find the contours from the thresholded image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#print("contours:",contours)
# draw all contours
for c in contours:
    if cv2.contourArea(c)>70000:
        continue


    (x, y, w, h) = cv2.boundingRect(c)
    #cv2.rectangle(image, (x,y), (x+w,y+h), (0, 255, 0), 2)

    ## BEGIN - draw rotated rectangle
    rect = cv2.minAreaRect(c)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    im=cv2.drawContours(image,[box],0,(255,255,255),-1)
    #im3=cv2.drawContours(im2,[box], 0, (255, 0, 0), 2)

# show the image with the drawn contours
plt.imshow(image)
#plt.imshow(im3)
#cv2.imwrite("textDectBox.png",im3)
cv2.imwrite("detectImg.png",im)

plt.show()

введите описание изображения здесь

...