rgba() 메서드에서 반환된 이 Mat가 RGBA 형식이 아닌 BGR 형식으로 나타나는 이유는 무엇입니까? (Why does this Mat returned by rgba() method appear to have BGR format rather than RGBA format?)


문제 설명

rgba() 메서드에서 반환된 이 Mat가 RGBA 형식이 아닌 BGR 형식으로 나타나는 이유는 무엇입니까? (Why does this Mat returned by rgba() method appear to have BGR format rather than RGBA format?)

여러 곳에서 읽었습니다(출처 , 소스) OpenCV는 기본적으로 BGR 색상 형식을 사용합니다.

하지만 이미지에서 특정 색상(빨간색)의 얼룩을 감지하는 클래스를 작성 중입니다(색상 얼룩 감지 샘플). 따라서 onCameraFrame(CvCameraViewFrame inputFrame) 함수에서 inputFrame.rgba()<값을 반환합니다. /코드>. 문서에 따르면 a>,

rgba() 이 메서드는 프레임이 있는 RGBA Mat를 반환합니다.

그래서 내 rgbaFrame 내 프로그램에서 inputFrame.rgba()의 값을 저장하는 변수인 에는 RGBA 형식의 Mat가 포함되어 있습니다.

하지만 앱을 실행하면 원본 이미지의 빨간색은 외부 SD 카드에 쓴 rgbaFrame Mat에서 파란색으로 나타납니다. 빨간색이 파란색으로 보이기 때문에 매트가 BGR 형식인 것 같습니다. (이 내용은 이 질문에 대한 의견입니다.)

그래서 cvtColor함수를

Imgproc.cvtColor(rgbaFrame, hsvImage, Imgproc.COLOR_RGB2HSV_FULL);

에서

6
Imgproc.cvtColor(rgbaFrame, hsvImage, Imgproc.COLOR_BGR2HSV_FULL);
로 변경했습니다.

하지만 프로그램을 실행할 때 아무것도 변경되지 않았습니다. 원본 이미지의 빨간색은 캡처된 프레임에서 여전히 파란색으로 나타납니다.

이제 RGB를 BGR 형식으로 변환하여 문제를 해결하는 데 도움이 되는지 확인하는 방법을 찾고 있습니다. 그러나 하나를 찾지 못했습니다. BGR을 RGB로 변환하려면 어떻게 해야 하나요? 다른 제안 사항이 있으면 공유해 주세요.


  • 참조 솔루션

    방법 1:

    OpenCV uses BGR by default, however, Android frame.rgba() implementation returns RGB (possibly for compliance with imageview and other Android components). However, the OpenCV function imwrite still requires BGR, therefore if you save the image without first converting it to BGR then the blue and red channels are saved incorrectly (swapped), because the Mat file of the frame has red channel in index 0 (RGB) whereas imwrite writes index 0 as blue (BGR). Similarly the frame has blue channel in index 2 whereas imwrite writes index 2 as red. You can call cvtcolor with COLOR_RGB2BGR before saving to a file.

    /**
     * Callback method that is called on every frame of the CameraBridgeViewBase class of OpenCV
     */
    override fun onCameraFrame(inputFrame: CameraBridgeViewBase.CvCameraViewFrame?): Mat {
        inputFrame?.let { currentFrame ‑>
    
            val currentFrameMat = currentFrame.rgba()
    
                // save the RGB2BGR converted version
                val convertedMat = Mat()
                Imgproc.cvtColor(currentFrameMat, convertedMat, Imgproc.COLOR_RGB2BGR)
                Imgcodecs.imwrite(imageFilePath, convertedMat)
    
            return currentFrameMat
        }
        return Mat()
    }
    

    (by Solacemcy)

    참조 문서

    1. Why does this Mat returned by rgba() method appear to have BGR format rather than RGBA format? (CC BY‑SA 2.5/3.0/4.0)

#image-processing #opencv4android #OpenCV #opencv3.0 #Android






관련 질문

이미지를 원통 또는 구 모양으로 매핑하시겠습니까? (Mapping image into cylinder or sphere shape?)

Android 마스킹 활동 만들기 (Android Creating a masking activity)

ImageJ 오버레이 ROI 줌 (ImageJ Overlay ROI zoom)

Virtex-5 FPGA 보드와 VGA 인터페이스 (interfacing VGA with Virtex-5 FPGA board)

C++ 이미지 처리, 입자 계산 (C++ image processing, counting particles)

훈련된 신경망을 사용하여 이미지에서 여러 객체를 식별하는 방법은 무엇입니까? (How do you use a trained neural net to identify multiple objects in an image?)

기계 학습을 사용하여 손으로 쓴 서명 이미지의 배경 제거 (Using machine learning to remove background in image of hand-written signature)

인수 '%s'에 대해 예상 Ptr<cv::UMat> 임계값을 지정하는 동안 오류가 발생했습니다. (getting an error while doing thresholding Expected Ptr<cv::UMat> for argument '%s')

iOS의 주어진 이미지에서 다중 사용자 정의 개체 감지(이미지 처리) (Multiple Custom Object Detection (Image processing) from a given Image in iOS)

두 개의 유사한 이미지에서 노이즈를 추출하는 방법은 무엇입니까? (How to extract noise from two similar images?)

OpenCV를 사용하여 이미지의 흰색 패치를 자르고 여권 크기의 사진을 만드는 방법 (How to crop white patches in image and make passport size photo using OpenCV)

OpenCV 그리기 특정 윤곽선 (OpenCV drawing specific contours)







코멘트