Reputation: 1648
I am trying to work with am RGB image in Opencv. From the image i only want to keep the red pixels and the rest i want to set to white. I am unsure how to do this logic in opencv. The image is read as Mat.
I wrote the following code but its not working.
Mat image;
for(i to rows)
for(j to col)
{
b=input[image.step * j + i]
g=input[image.step * j + i + 1]
r=input[image.step * j + i + 2]
if(r == 255 && g&b == 0)
{
image.at<Vec3f>(j,i)=img.at<Vec3F>(j,i)
}
else image.push_back(0);
This is the code i wrote
I am sure its incorrect but I am unable to do it. Can i get some help
Upvotes: 1
Views: 3121
Reputation: 497
You would like to keep only those pixels which are purely red, i.e. red is 255 and green, and blue is zeros. Basically, you want to change those pixels which does not satisfy that condition:
if(~(red == 255 and green == 0 and blue == 0))
red = green = blue = 255
Below is the code in python:
img = cv2.imread(filename)
rows , cols , layers = img.shape
for i in range(rows):
for j in range(cols):
if(~(img[i][j][2] == 255 and img[i][j][0] == 0 and img[i][j][1] == 0)):
img[i][j][0] = img[i][j][1] = img[i][j][2] = 255
Upvotes: 3