Ask Your Question
1

i want to change the black pixels in the image to Red

asked 2016-08-20 21:34:15 -0600

Mohamed_Elsayed gravatar image

updated 2016-08-20 23:42:26 -0600

berak gravatar image

i faced a problem with this i want to write this line in python

img_rgb .setTo(cv::Scalar(0,0,255), mask);

this the input image : http://i.stack.imgur.com/FjEXu.jpg

this is the masking image : http://i.stack.imgur.com/wWIcR.png

this is the output desired image : http://i.stack.imgur.com/vhb67.png

and my Code this :

import numpy as np import imutils

import cv2

img_rgb = cv2.imread('figi.jpg')

Conv_hsv_Gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

ret, mask = cv2.threshold(Conv_hsv_Gray, 0, 255,cv2.THRESH_BINARY_INV |cv2.THRESH_OTSU)

img_rgb.Set(mask,cv2.Scalar(0,0,255)) #the problem

cv2.imshow("imgOriginal", img_rgb)

cv2.imshow("output", res)

cv2.imshow("mask", mask)

cv2.waitKey(0)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-08-22 17:04:15 -0600

harsha gravatar image

img_rgb is a numpy ndarray.

To find indices of the mask

indices = np.where(mask==255)

To fill pixels at these indices with red color

img_rgb[indices[0], indices[1], :] = [0, 0, 255]

img_rgb now has red pixels

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-08-20 21:31:46 -0600

Seen: 12,902 times

Last updated: Aug 22 '16