问题描述
如何在python中制作它?我在这条线上遇到了问题
how can you make it in python? I faced a problem with this line
img_rgb.Set(mask,cv2.Scalar(0,0,255))
这是代码:
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)
推荐答案
mat.setTo()
的API在python的Opencv模块中不可用,这是由于在C++
中Opencv使用cv::Mat
对象作为图像处理的基本实体,但是在Python中没有这样的cv::Mat
概念,相反,Opencv的Python API使用众所周知的库numpy
进行图像处理操作,并且numpy具有非常漂亮的语法,可以使用掩码设置值:
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]
只需用img_rgb.Set(mask,cv2.Scalar(0,0,255))
替换此行即可完成工作.
Simply replacing this line with the img_rgb.Set(mask,cv2.Scalar(0,0,255))
would get your work done.
这篇关于如何使用opencv python将黑色更改为红色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!