2016-03-15 36 views
0

我試圖在圖像中灰化所有人的面孔。雖然我可以檢測到他們的臉並將它們灰成較小的墊子,但我無法將灰色的臉部「複製」到原始墊子上。這樣最終的結果將會有一張灰色的臉。OpenCV檢測投資回報率,創建子圖並複製到原始圖塊

 faceDetector.detectMultiScale(mat, faceDetections); 
     for (Rect rect : faceDetections.toArray()) 
     { 
      Rect rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height); 
      Mat imageROI = new Mat(mat,rectCrop); 

      //convert to B&W 
      Imgproc.cvtColor(imageROI, imageROI, Imgproc.COLOR_RGB2GRAY); 

      //Uncomment below will grayout the faces (one by one) but my objective is to have them grayed out on the original mat only. 
      //Highgui.imwrite(JTestUtil.DESKTOP_PATH+"cropImage_"+(++index)+".jpg",imageROI); 

      //add to mat? doesn't do anything :-(
      mat.copyTo(imageROI); 
     } 

回答

1

imageROI是3或4通道圖像。 cvtColor to gray給出單通道輸出,並且imageROI對mat的引用可能被破壞。

使用緩衝區進行灰度轉換,並將dst作爲imageROI轉換回RGBA或RGB。

faceDetector.detectMultiScale(mat, faceDetections); 
    for (Rect rect : faceDetections.toArray()) 
    { 
     Rect rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height); 
     //Get ROI 
     Mat imageROI = mat.submat(rectCrop); 

     //Move this declaration to onCameraViewStarted 
     Mat bw = new Mat(); 

     //Use Imgproc.COLOR_RGB2GRAY for 3 channel image. 
     Imgproc.cvtColor(imageROI, bw, Imgproc.COLOR_RGBA2GRAY); 
     Imgproc.cvtColor(bw, imageROI, Imgproc.COLOR_GRAY2RGBA); 
    } 

結果看起來像enter image description here

+0

感謝@Vasanth的答案。給定一幅面部圖像,我的目標是將圖像中的所有面轉換爲B&W。在我的代碼中,我能夠逐個轉換面部(如圖像),但無法將它們全部放在原始圖像中。再次感謝! (我認爲問題出在你寫的頻道的某個地方) – adhg

+0

如果它解決了你的問題,請接受它。可能對別人也有用:)。 – Vasanth

相關問題