I have a program written in python using OpenCV in which I am trying to color a group of pixels. The condition is that if there is more than 1000 continuous pixels, then all those 1000 pixels must get colored with a different color, and for this example, that color is red.
The code:
import cv2
import numpy as np
print("Package Imported")
coordinate = x, y = 150, 69
imgColor = cv2.imread(r"D:\Downloads\residence-g1678df24f_1280.jpg")
img = cv2.imread(
r"D:\Downloads\residence-g1678df24f_1280.jpg", 0)
res, img2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
l = len(img2)
w = len(img2[0])
print(l, w)
count = 1
y=0
x=0
sX=0
sY=0
for x in range(0,l):
imgColor[0,x]=(0,0,255)
for y in range(0,w):
if img2[x,y]==255:
count=count+1
else:
count=1
sX = x
sY = y
print(img2[x,y],x,y)
if count > 1000:
print("Coloring from ", sX, sY, "to ", x, y)
for i in range(sX, x):
for j in range(sY, y):
imgColor[i, j] = (0, 0, 255)
count=1
cv2.imshow("GrayScale Image", img)
cv2.imshow("Photo", img2)
cv2.imshow("Color Image", imgColor)
cv2.imwrite("D:/VS Projects/openCVTEST/ColoredImage.jpg", imgColor)
cv2.imwrite("D:/VS Projects/openCVTEST/ThresholdImage.jpg", img2)
cv2.imwrite("D:/VS Projects/openCVTEST/GrayScaleImage.jpg",img)
cv2.waitKey(0)
Basically, I first threshold the image and then use an if operator to check id the current pixel is white. If the pixel is white, then the variable count is incremented. If not, then count is reset back to 0 and sX and sY are marked as the starting coordinates. Once count exceeds 1000, then another for loop runs, coloring imgColor for (sX,sY) to the current pixel (x,y).
However, the results that I am getting are like this:
ImgColor:
img2 (Threshold):
Ideally, shouldn't a lot more white space (such as the whitespace on the left of the house) be colored red as well? Since the thresholded image has only 255 or 0, a lot more of the 255 should also be colored red. Is my logic itself wrong, or am I missing something? I changed the min count (count > 100) to 100, but even that is producing similar results.



