Coloring Continuous set of Pixels

Viewed 48

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:

enter image description here

img2 (Threshold):

enter image description here

img (Grayscale): enter image description here

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.

1 Answers

If you want to achieve this effect, you can continue to look at my following example code

enter image description here

import cv2

# read the picture
img_color = cv2.imread(r"C:\Users\SY\Desktop\test\A.jpg", 1)
img_gray = cv2.imread(r"C:\Users\SY\Desktop\test\A.jpg", 0)
# get img_hight and img_width
img_hight = len(img_color)
img_width = len(img_color[0])

# use the key to change the result
threshold_key = 150
img_gray = cv2.threshold(img_gray, threshold_key, 255, cv2.THRESH_BINARY)[1]

color_one = (0,0,255)
for hight in range(0, img_hight):
    flag_width = 0
    for width in range(0, img_width):
        if img_gray[hight, width] == 0:  
            if width - flag_width  > 200 :
                for w in range(flag_width, width):
                    img_color[hight, w] = color_one
            flag_width = width
        
        # if the whole line is whithe
        if (width == img_width-1) and (flag_width == 0) :
            for w in range(width+1): 
                img_color[hight, w] = color_one
cv2.imwrite("A.bmp", img_color)
Related