Adi*_*vit 92
使用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);
Run Code Online (Sandbox Code Playgroud)
img.setTo(cv::Scalar(redVal,greenVal,blueVal));
Run Code Online (Sandbox Code Playgroud)
Kim*_*mmo 23
以下是如何在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)
Run Code Online (Sandbox Code Playgroud)
这里有更完整的示例如何创建填充了某种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)
Run Code Online (Sandbox Code Playgroud)
X''*_*X'' 10
最简单的是使用OpenCV Mat类:
img=cv::Scalar(blue_value, green_value, red_value);
Run Code Online (Sandbox Code Playgroud)
在哪里img定义为cv::Mat.
创建一个新的640x480图像,并用紫色(红色+蓝色)填充它:
cv::Mat mat(480, 640, CV_8UC3, cv::Scalar(255,0,255));
Run Code Online (Sandbox Code Playgroud)
注意:
使用numpy.full. 这是一个创建灰色、蓝色、绿色和红色图像并显示在 2x2 网格中的 Python。
import cv2
import numpy as np
gray_img = np.full((100, 100, 3), 127, np.uint8)
blue_img = np.full((100, 100, 3), 0, np.uint8)
green_img = np.full((100, 100, 3), 0, np.uint8)
red_img = np.full((100, 100, 3), 0, np.uint8)
full_layer = np.full((100, 100), 255, np.uint8)
# OpenCV goes in blue, green, red order
blue_img[:, :, 0] = full_layer
green_img[:, :, 1] = full_layer
red_img[:, :, 2] = full_layer
cv2.imshow('2x2_grid', np.vstack([
np.hstack([gray_img, blue_img]),
np.hstack([green_img, red_img])
]))
cv2.waitKey(0)
cv2.destroyWindow('2x2_grid')
Run Code Online (Sandbox Code Playgroud)
color=(200, 100, 255) # sample of a color
img = np.full((100, 100, 3), color, np.uint8)
Run Code Online (Sandbox Code Playgroud)
对于8位(CV_8U)OpenCV映像,语法为:
Mat img(Mat(nHeight, nWidth, CV_8U);
img = cv::Scalar(50); // or the desired uint8_t value from 0-255
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
118569 次 |
| 最近记录: |