본문 바로가기
Open CV

opencv c++ gray scale, HSV scale 적용하기

by 빈이쥬 2023. 1. 13.

grayscale

변환하는 함수는 다음과 같다

cvtColor(frame,gray,COLOR_RGB2GRAY);

void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )
  • src – 입력 이미지: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), 또는 single-precision floating-point.
  • dst – 입력 이미지와 동일한 크기와 뎁스(depth)의 출력 이미지.
  • code – 컬러 공간 변환 코드.
  • dstCn – 최종 이미지의 채널 수; 파라미터가 0이라면 채널 수는 자동으로 src와 code로부터 계산

HSV 스케일링

그레이 스케일과 같은 방법을 사용하며, cvtColor 마지막 인수만 다르다.

HVS는 Hue(색상), saturation(채도), value(명도)를 뜻함.

HSV 색상 모델을 사용하면 색상 정보를 밝기 정보와 분리하여 색상을 기준으로 이미지를 더 쉽게 분할할 수 있기 때문에 색상을 기준으로 임계값을 지정해야 하는 이미지로 작업하는 경우 유용할 수 있다.

 

cvtColor(frame,gray,COLOR_RGB2HSV);

 

cvtColor docs 참고

https://docs.opencv.org/4.7.0/de/d25/imgproc_color_conversions.html

 

OpenCV: Color conversions

See cv::cvtColor and cv::ColorConversionCodes Todo:document other conversion modes RGB \(\leftrightarrow\) GRAY Transformations within RGB space like adding/removing the alpha channel, reversing the channel order, conversion to/from 16-bit RGB color (R5:G6

docs.opencv.org

 

 

 

소스 코드

#include <opencv2/opencv.hpp>
#include <iostream>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    
    string url = "http://192.168.0.104";
    
    // Open the default camera
    VideoCapture cap(url);

    // Check if the camera is opened
    if(!cap.isOpened())
    {
        cout << "Error opening camera" << endl;
        return -1;
    }
 
    // Capture frames from the camera in a loop
    while(true)
    {
        // Capture a frame
        Mat frame;
        cap >> frame;

        // Check if the frame is empty
        if(frame.empty())
        {
            cout << "Empty frame" << endl;
           break;
        }
        //grayscale
        Mat gray;
        cvtColor(frame,gray,COLOR_RGB2GRAY);
        
        //HSV scale
        //cvtColor(frame,gray,COLOR_RGB2HVS);
        
        // Display the frame
        imshow("Frame", gray);

        // Press 'q' to exit the loop
        if(waitKey(1) == 'q')
        {
            break;
        }
    }

    // Release the camera
    cap.release();

    return 0;
}

 

소스리뷰

#include <opencv2/imgproc.hpp> 

회색 변환을 위한 헤더파일을 추가한다.

 

cvtColor(frame,gray,COLOR_RGB2GRAY);

cvtColor(frame,gray,COLOR_RGB2HSV);

cvtColor 함수를 사용해 frame를 입력 영상으로 gray를 출력영상으로 사용하고,  COLOR_RGB2GRAY,COLOR_RGB2HSV색상을 변환한다.

 

 

스트리밍 영상출력 소스 설명.

https://program-s.tistory.com/43

 

OPENCV C++ 스트리밍 영상 출력하기

esp32 cam 영상 출력 소스는 아래를 참고하자. 최종 소스 - ESP32 CAM의 영상을 출력 #include #include using namespace cv; using namespace std; int main(int argc, char* argv[]) { string url = "http://192.168.0.104"; // Open the default c

program-s.tistory.com

 

출력

변환전

grayscale

HSV 스케일링