如何用一个纯色填充OpenCV图像?
如何用一个纯色填充OpenCV图像?
在IplImage* img使用OpenCV C API IplImage* img : 
 使用cvSet() : cvSet(img, CV_RGB(redVal,greenVal,blueVal)); 
 使用cv::Mat img的OpenCV C ++ API,然后使用: 
  cv::Mat::operator=(const Scalar& s)如下所示: 
 img = cv::Scalar(redVal,greenVal,blueVal); 
 或更一般的,面具支持, cv::Mat::setTo() : 
 img.setTo(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) 
这里有更完整的例子来说明如何创build一个新的空白图像填充一定的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) 
最简单的就是使用OpenCV Mat类:
 img=cv::Scalar(blue_value, green_value, red_value); 
 其中img被定义为cv::Mat 。 
对于8位(CV_8U)的OpenCV映像,其语法是:
 Mat img(Mat(nHeight, nWidth, CV_8U); img = cv::Scalar(50); // or the desired uint8_t value from 0-255 
创build一个新的640×480图像,并填充紫色(红色+蓝色):
 cv::Mat mat(480, 640, CV_8UC3, cv::Scalar(255,0,255)); 
注意:
- 宽度之前的高度
- typesCV_8UC3表示8位无符号整数,3个通道
- 颜色格式是BGR
如果您使用OpenCV的Java,那么您可以使用下面的代码。
 Mat img = src.clone(); //Clone from the original image img.setTo(new Scalar(255,255,255)); //This sets the whole image to white, it is R,G,B value