如何用一种纯色填充图像
如何使用一种纯色填充图像?有大佬知道的吗
使用OpenCV C API IplImage* img:
使用cvSet():cvSet(img, CV_RGB(redVal,greenVal,blueVal));
使用OpenCV C ++ API cv::Mat img 然后使用以下任一方法:
cv::Mat:: operator=(const Scalar& s) 如下所示:
img = cv::Scalar(redVal,greenVal,blueVal); 以下是如何在Python中使用cv2:
# Create a blank 300x300 black image
image = np.zeros((300, 300, 3), np.uint8)
# Fill image with red color(set each pixel to red)
image[:] = (0, 0, 255)
下面是更完整的示例,了解如何创建填充了某种RGB颜色的新空白图像:
import cv2
import numpy as np
def create_blank(width, height, rgb_color=(0, 0, 0)):
"""Create new image(numpy array) filled with certain color in RGB"""
# Create black blank image
image = np.zeros((height, width, 3), np.uint8)
# Since OpenCV uses BGR, convert the color first
color = tuple(reversed(rgb_color))
# Fill image with color
image[:] = color
return image
# Create new blank 300x300 red image
width, height = 300, 300
red = (255, 0, 0)
image = create_blank(width, height, rgb_color=red)
cv2.imwrite('red.jpg', image)
页:
[1]