How to change black color to Red with OpenCV Python?
Asked Answered
O

1

6

How can you make it in Python? I faced a problem with this line

img_rgb.Set(mask,cv2.Scalar(0,0,255))

and here is the code :

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))

cv2.imshow("imgOriginal", img_rgb)  # show windows

cv2.imshow("output", res)  # show windows

cv2.imshow("mask", mask)  # show windows

cv2.waitKey(0)
Oligarch answered 20/8, 2016 at 19:56 Comment(4)
what was the problem? the red is actually blue? crash? tell us more.Languedoc
cv2.THRES_OTSU will determine the threshold automatically and probably it will not be pure black color. Try ret, mask = cv2.threshold(Conv_hsv_Gray, 0, 255,cv2.THRESH_BINARY_INV) or ret, mask = cv2.threshold(Conv_hsv_Gray, 1, 255,cv2.THRESH_BINARY_INV) (not sure whether thres value is inclusive or exclusive) instead. Please post your input image too, jpeg might introduce some artifacts so that "black" might not be "pure black".Devora
the input image is : i.sstatic.net/FjEXu.jpg and the output image is : i.sstatic.net/vhb67.png im changing the black color in the image and Convert it to the Red color cuz the intensty of the Red Color is more than the black so After the changing it will easy to track the black objects without eny disturbOligarch
I remember that image. since it is not perfect black the OTSU version will be good ;)Devora
P
13

The API for mat.setTo() is not available in Opencv module for python, this is due to the reason that in C++ Opencv uses cv::Mat object as basic entity for image manipulation, However in Python there is no such cv::Mat concept, instead Python API for Opencv uses the well known library numpy for image manipulation operations, and numpy has a very beautiful syntax to set the values using a mask:

img_rgb[mask == 255] = [0, 0, 255]

Simply replacing this line with the img_rgb.Set(mask,cv2.Scalar(0,0,255)) would get your work done.

Penrod answered 21/8, 2016 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.