I would like to fill these polygon with white color and this operation is repeated through a loop over the whole image so I would like to know the syntax and function which can be used to perform this operation using opencv in python This is my input image
alist=[]
img = cv2.imread('closing2.jpg',cv2.IMREAD_GRAYSCALE)
imo = cv2.imread('closing2.jpg',cv2.IMREAD_GRAYSCALE)
imr = cv2.imread('closing2.jpg',cv2.IMREAD_GRAYSCALE)*0
imac = imr
height , width = imo.shape[:2]
a,im = cv2.threshold(img,200,255,cv2.THRESH_BINARY)
# i=100
# p=[i,i]
points = []
for j in range(0,1000,50):
for i in range(0,1000,50):
p=[i,j]
poly = raypoly(im,p,5)
st = metrics(p,poly)
polyc=raypolyLimit(im,p,st,30)
# print(polyc)
# print(len(polyc))
# for m in range(len(polyc)):
# point = polyc[m]
# cv2.fillConvexPoly(im, point, 255)
plotpoly(polyc,imr)
plotpolypoints(polyc,imr,255,1)
# plotray(im,p,imr)
am = polyArea(polyc)
# print(am)
if am > 5:
alist.append(am)
# img[(i-5):(i+5),(j-5):(j+5)]=150
# plt.imshow(imf)
# plt.show()
imo = im * 0
plotpoly(polyc,imo)
plotpoly(polyc, im)
# plotArea(polyc,imo)
imf = imo*1000 +im
a,imf = cv2.threshold(imf,100.0,255.0,cv2.THRESH_BINARY)
cv2.imshow('Frame_1', imr)
cv2.imshow('Frame_3', imo)
cv2.imshow('Frame_4', imf)
cv2.imshow('Frame_5', im)
cv2.waitKey(1)
I used the following function and the result as follows:
cv2.floodFill(imo,None,(i,j),255)
But my problem is that it gives a white flash for the whole image after certain number of polygons and I don't know how to fix it .
h contains fillpoly() function which is used to draw and fill a polygon i.e. triangle, rectangle, pentagon, hexagon etc. It require same arguments as drawpoly(). Syntax : void fillpoly( int number, int *polypoints ); where, number indicates (n + 1) number of points where, n is the number of vertices in a polygon.
To draw shapes using polylines() method we have to pass five arguments are our blank image, coordinates of shape, True( true if you want closed shape otherwise, false for open from the first set of coordinates to last one), the color of shape(in RGB format) and the thickness of the shape.
You can use cv2.drawContours()
to fill in a contour. From the docs, if the thickness
parameter is negative (thickness=CV_FILLED
or thickness=-1
) then the contours will be filled. For instance, to fill in a contour with white
cv2.drawContours(image, [c], -1, (255,255,255), -1)
So the idea is to find the desired contour using cv2.findContours()
then fill it in using cv2.drawContours()
. For your image:
import cv2
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (255,255,255), -1)
cv2.imshow('image', image)
cv2.waitKey()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With