Come riempire l’immagine OpenCV con un colore solido?
Utilizzando l’API OpenCV C con IplImage* img
:
Usa cvSet () : cvSet(img, CV_RGB(redVal,greenVal,blueVal));
Utilizzando l’API OpenCV C ++ con cv::Mat img
, quindi utilizzare uno:
cv::Mat::operator=(const Scalar& s)
come in:
img = cv::Scalar(redVal,greenVal,blueVal);
o il più generale, maschera di supporto, cv::Mat::setTo()
:
img.setTo(cv::Scalar(redVal,greenVal,blueVal));
Ecco come fare con cv2 in Python:
# 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)
Ecco un esempio più completo su come creare una nuova immagine vuota riempita con un determinato colore 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)
Il più semplice sta usando la class Mat OpenCV:
img=cv::Scalar(blue_value, green_value, red_value);
dove img
era definito come cv::Mat
.
Per un’immagine OpenCV a 8 bit (CV_8U), la syntax è:
Mat img(Mat(nHeight, nWidth, CV_8U); img = cv::Scalar(50); // or the desired uint8_t value from 0-255
Crea una nuova immagine 640×480 e riempila di viola (rosso + blu):
cv::Mat mat(480, 640, CV_8UC3, cv::Scalar(255,0,255));
Nota:
Se stai usando Java per OpenCV, puoi usare il seguente codice.
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