如何正确转换JavaCV中的OpenCV代码(mixChannels)

时间:2016-10-18 12:59:45

标签: opencv javacv

我的目标与询问in this post

的人的目标相同
  

我有JPEG和面具。我想用三个JPEG创建一个PNG   通道和alpha通道应该是Mask。我怎样才能实现   这与OpenCV?

但我不知道如何将C ++代码转换为JavaCV。我所有的尝试都没有成功

这是C ++代码:

crcSlow(unsigned char const message[], int nBytes)
{
crc            remainder = INITIAL_REMAINDER;
int            byte;
unsigned char  bit;
/* Perform modulo-2 division, a byte at a time. */
for (byte = 0; byte < nBytes; ++byte)
{
    /* Bring the next byte into the remainder. */
    remainder ^= (REFLECT_DATA(message[byte]) << (WIDTH - 8));
    /* Perform modulo-2 division, a bit at a time. */
    for (bit = 8; bit > 0; --bit)
    {
        /*Try to divide the current data bit.  */
        if (remainder & TOPBIT)
        {
            remainder = (remainder << 1) ^ POLYNOMIAL;
        }
        else
        {
            remainder = (remainder << 1);
        }
    }
}

/* The final remainder is the CRC result. */
return (REFLECT_REMAINDER(remainder) ^ FINAL_XOR_VALUE);

}   

我的实施是:

cv::Mat transparent( height, width, CV_8UC4);
cv::Mat srcImg[] = {JPEG_img, alpha_Mask};
int from_to[] = { 0,0, 1,1, 2,2, 3,3 };
cv::mixChannels( srcImg, 2, &transparent, 1, from_to, 4 );

但是我收到了这个错误:

IplImage effect = opencv_imgcodecs.cvLoadImage(pathToEffect, opencv_imgcodecs.CV_LOAD_IMAGE_COLOR);
IplImage mask = opencv_imgcodecs.cvLoadImage(pathToMask, opencv_imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);

CvMat matEffect = new CvMat(effect);
CvMat matMask = new CvMat(mask);
CvMat transparent = new CvMat(matEffect.size());

int from_to[] = { 0,0, 1,1, 2,2, 3,3 };

org.bytedeco.javacpp.helper.opencv_core.CvArr[] srcImg = {matEffect, matMask};
org.bytedeco.javacpp.helper.opencv_core.CvArr[] dstImg = {transparent};

opencv_core.cvMixChannels(srcImg, 2, dstImg, 1, from_to, 4);

1 个答案:

答案 0 :(得分:0)

不幸的是,我没有在这台计算机上安装过OpenCV,但是我已经将C ++版本翻译成了Java。它应该工作。如果Core.mixchannels出错,请尝试更改为cv ::。

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class Hello
{
   public static void main( String[] args )
   {

    Mat transparent( height, width, CV_8UC4);
    Mat [] srcImg = {JPEG_img, alpha_Mask};
    int [] from_to = { 0,0, 1,1, 2,2, 3,3 };
    //Core.mixChannels( srcImg, 2, &transparent, 1, from_to, 4 );
cvMixChannels(srcImg, 2, &transparent, 1, from_to, 4); // edit
       }
    }