简体   繁体   English

如何使用opencv python将黑色更改为红色

[英]how to change the black color to Red with opencv python

how can you make it in python? 如何在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)

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: 用于mat.setTo()的API在mat.setTo()模块中不可用,这是由于以下原因:在C++ Opencv使用cv::Mat对象作为图像处理的基本实体,但是在Python中没有这样的cv::Mat概念,而不是Opencv的Python API使用众所周知的库numpy进行图像处理操作,并且numpy具有非常漂亮的语法,可以使用掩码设置值:

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. 只需用img_rgb.Set(mask,cv2.Scalar(0,0,255))替换此行img_rgb.Set(mask,cv2.Scalar(0,0,255))完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM