2017-04-16 204 views
0

我試圖將RGB圖像(http://www.kavim.com/uploads/signs/tn__51A2376.JPG) 拆分爲單獨的通道,對每個通道進行閾值並將其合併回來,從而導致類似的情況。將合併的RGB圖像轉換爲灰度OPENCV ANDROID

enter image description here

我不明白的是,爲什麼,如果我嘗試合併後的圖像轉換爲灰度我得到這個顏色閾值輸出(而不是灰度圖像代替)。

public Mat onCameraFrame(CvCameraViewFrame inputFrame) { 
     Mat rgba = inputFrame.rgba(); 
     Size sizeRgba = rgba.size(); 
     Mat grayInnerWindow1; 
     Mat rgbaInnerWindow; 

     int rows = (int) sizeRgba.height; 
     int cols = (int) sizeRgba.width; 

     int left = cols/8; 
     int top = rows/8; 

     int width = cols * 3/4; 
     int height = rows * 3/4; 

      rgbaInnerWindow = rgba.submat(top, top + height, left, left + width); 

     ArrayList<Mat> channels = new ArrayList<Mat>(3); 

     Mat src1= Mat.zeros(rgbaInnerWindow.size(),CvType.CV_8UC3); 

     Core.split(rgbaInnerWindow, channels); 

     Mat b = channels.get(0); 
     Imgproc.threshold(b, b, 0, 70, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU); 
     Mat g = channels.get(1); 
     Imgproc.threshold(g, g, 0, 70, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU); 
     Mat r = channels.get(2); 
     Imgproc.threshold(r, r, 90, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU); 

     Core.merge(channels, src1); 
     Imgproc.medianBlur(src1, src1, 3); 

     Imgproc.threshold(src1,rgbaInnerWindow,0, 255, Imgproc.THRESH_BINARY); 
     Imgproc.cvtColor(rgbaInnerWindow,src1, Imgproc.COLOR_BGR2GRAY); 

       rgbaInnerWindow.release(); 

      return rgba; 
     } 
    } 

回答

0

問題是,我試圖將設置爲inputFrame.rgba()的對象設置爲灰色。

爲此,我必須創建另一個mat set作爲inputFrame.gray(),然後嘗試將其轉換。

Mat rgb = inputFrame.rgba(); 
Mat gray = inputFrame.gray(); 
Mat grayInnerWindow = gray.submat(top, top + height, left, left + width); 
Mat rgbaInnerWindow = rgba.submat(top, top + height, left, left + width); 
//SOME CODE 
Imgproc.cvtColor(rgbaInnerWindow,grayInnerWindow, Imgproc.COLOR_BGR2GRAY); 

現在這樣工作,因爲它應該!

相關問題