TechieBoy101
TechieBoy101

Reputation: 304

stuck on color change in OpenCV

I am just a newbie in OpenCV. I was testing a program to convert all green pixels of an image to white.

Input:

I have chosen this sample image:

and this is my python program to do it.

import cv2
import numpy as np

img=cv2.imread('exp.jpg',cv2.IMREAD_COLOR)
row,cols,layer=img.shape


for i in range (row):
    for j in range (cols):
     a=img[i,j]
     if(a[1]>128):
      img[i, j] = [255, 255, 255]
cv2.imshow('a',img)


cv2.waitKey(0)
cv2.destroyAllWindows()

The program works pretty fine but some part of the desired picture is lost too.

This is the output image

I dont know what went wrong.I even checked the BGR value at that pixel by

print(img[432,144])

and it's output was [ 37 209 37].I don't think its the actual color there as when I use them in rgbtohex.net to view that color, it was nothing like it. I don't know where it went wrong with that. Pardon me if I am wrong about anything.

Upvotes: 1

Views: 794

Answers (1)

Łukasz Szczesiak
Łukasz Szczesiak

Reputation: 214

You have to rethink your if statment for it to work better with those images, and not include unwanted pixels. For exemaple [0 255 255] is Cyan but it will also will get included cus if(a[1]>128): so a[1] = 255 and if 255 > 128.

Now we know that we want G to be high valued and R or B low valued, example solution would be if(a[1] > 200 and a[0] < 128 and a[2] < 128).

Of course my example may work even worse then your on a diffrent image, that why you want to use some image processing tools first.You may learn more about it here image procesing

Upvotes: 1

Related Questions