2017-10-05 13 views
0

JavaCVを使用して画像のサイズを変更するコードがあり、画像の透明な背景領域を白に変更する必要があります。 ここに私のコードです、私はCOLOR_RGBA2RGBまたはCOLOR_BGRA2BGRでcvtColor()を使ってみましたが、結果は黒い背景のimageです。 JavaCVでpng透過層を白に変更するには

void myFnc(byte[] imageData){ 
     Mat img = imdecode(new Mat(imageData),IMREAD_UNCHANGED); 
     Size size = new Size(newWidth, newHeight); 
     Mat whbkImg = new Mat(); 
     cvtColor(img, whbkImg, COLOR_BGRA2BGR); 
     Mat destImg = new Mat(); 
     resize(whbkImg,destImg,size); 

     IntBuffer param = IntBuffer.allocate(6); 
     param.put(CV_IMWRITE_PNG_COMPRESSION); 
     param.put(1); 
     param.put(CV_IMWRITE_JPEG_QUALITY); 
     param.put(100); 
     imwrite(filePath, destImg, param); 
} 
+0

あなたの画像を投稿してください – Silencer

+0

私は、テキスト – Reza

答えて

1

あなたはすなわちalphaは、この答えはに基づいています0(透明)

すると仮定255RGBチャネルを設定し、白にRGBの色を設定する必要があります:Change all white pixels of image to transparent in OpenCV C++

// load image and convert to transparent to white 
Mat inImg = imread(argv[1], IMREAD_UNCHANGED); 
if (inImg.empty()) 
{ 
    cout << "Error: cannot load source image!\n"; 
    return -1; 
} 

imshow ("Input Image", inImg); 

Mat outImg = Mat::zeros(inImg.size(), inImg.type()); 

for(int y = 0; y < inImg.rows; y++) { 
    for(int x = 0; x < inImg.cols; x++) { 
     cv::Vec4b &pixel = inImg.at<cv::Vec4b>(y, x); 
     if (pixel[3] < 0.001) { // transparency threshold: 0.1% 
      pixel[0] = pixel[1] = pixel[2] = 255; 
     } 
     outImg.at<cv::Vec4b>(y,x) = pixel; 
    } 
} 

imshow("Output Image", outImg); 

return 0; 

あなたがここに上記のコードをテストすることができます。http://www.techep.csi.cuny.edu/~zhangs/cv.html

javacvについては、以下のコードは同等だろうが(私はまだテストしていない)

Mat inImg = imdecode(new Mat(imageData),IMREAD_UNCHANGED); 
Mat outImg = Mat.zeros(inImg.size(), CV_8UC3).asMat(); 

UByteIndexer inIndexer = inImg.createIndexer(); 
UByteIndexer outIndexer = outImg.createIndexer(); 

for (int i = 0; i < inIndexer.rows(); i++) { 
    for (int j = 0; j < inIndexer.cols(); i++) { 
     int[] pixel = new int[4]; 
     try { 
      inIndexer.get(i, j, pixel); 
      if (pixel[3] == 0) { // transparency 
       pixel[0] = pixel[1] = pixel[2] = 255; 
      } 
      outIndexer.put(i, j, pixel); 
     } catch (IndexOutOfBoundsException e) { 

     } 
    } 
} 
+0

こんにちはドゥックで画像のURLを入れソリューションのおかげで、それが働いています私はそれのためのパフォーマンスの場合にはより良い方法があるのだろうか? – Reza

+0

透明ピクセルが白であるかどうかはわからないので、おそらくピクセルを白に設定する必要がありますか? (透明ピクセルは任意の色にできますが、それでも透明です。) –

関連する問題