Program Club

OpenCV 이미지를 단색으로 채우는 방법은 무엇입니까?

proclub 2020. 11. 10. 22:31
반응형

OpenCV 이미지를 단색으로 채우는 방법은 무엇입니까?


OpenCV 이미지를 단색으로 채우는 방법은 무엇입니까?


다음과 함께 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);

또는 더 일반적인 마스크 지원,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)

다음은 특정 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);

여기서, imgA와 정의 하였다 cv::Mat.


새 640x480 이미지를 만들고 자주색 (빨강 + 파랑)으로 채 웁니다.

cv::Mat mat(480, 640, CV_8UC3, cv::Scalar(255,0,255));

노트 :

  • 너비 전 높이
  • CV_8UC3 유형은 8 비트 unsigned int, 3 채널을 의미합니다.
  • 색상 형식은 BGR입니다.

8 비트 (CV_8U) OpenCV 이미지의 경우 구문은 다음과 같습니다.

Mat img(Mat(nHeight, nWidth, CV_8U);
img = cv::Scalar(50);    // or the desired uint8_t value from 0-255

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

Use numpy.full. Here's a Python example that sets the whole image to gray and ensures an unsigned 8-bit integer result type.

import cv2
import numpy as np
img = np.full((100, 100, 3), 127, np.uint8) 
cv2.imshow('single color', img)
cv2.waitKey(0)
cv2.destroyWindow('single color')

참고URL : https://stackoverflow.com/questions/4337902/how-to-fill-opencv-image-with-one-solid-color

반응형