我正在尝试将二进制图像掩盖到原始图像上,大多数情况下它都可以工作,但是我偶尔会得到这个:
关于为什么会发生这种情况以及如何解决它的任何想法?提前致谢 :)
编辑:好的,所以我有 2 张图像,我发现它们之间的区别,最终是手。
好结果的例子
然后我将差异图像转换为灰度,将图像二值化(使用 cvThreshold),最后将结果用作 input2 上的掩码(带有手的原始图像)。最后的操作 cvCopy 似乎是造成这种撕裂的原因,请注意它有时只会发生(大约 1/5 次)。
这是我的 Main.java 的样子:
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_imgproc.CvHistogram;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
public class Main {
private static CanvasFrame AbsDiffFrame = new CanvasFrame("cvAbsDiff");
private static CanvasFrame SubDiffFrame = new CanvasFrame("cvSub");
private static CanvasFrame grayscale = new CanvasFrame("cvCvtColor CV_RGB2GRAY");
private static CanvasFrame binary = new CanvasFrame("cvThreshold CV_THRESH_BINARY");
private static CanvasFrame hsv = new CanvasFrame("cvCvtColor CV_RGB2HSV");
private static CanvasFrame cvMultiplyAcc = new CanvasFrame("cvMultiplyAcc");
private static CanvasFrame hsv2 = new CanvasFrame("cvCvtColor CV_RGB2HSV");
private static IplImage input1;
private static IplImage input2;
public static void main(String[] args) {
// TODO Auto-generated method stub
AbsDiffFrame.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
SubDiffFrame.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
grayscale.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
binary.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
hsv.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
hsv2.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
cvMultiplyAcc.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
input1 = cvLoadImage("input1.jpg",CV_LOAD_IMAGE_COLOR);
input2 = cvLoadImage("input2.jpg",CV_LOAD_IMAGE_COLOR);
ImageProcessor imgProc = new ImageProcessor();
AbsDiffFrame.showImage(imgProc.getAbsDifference(input1, input2));
IplImage img = imgProc.getSubDifference(input2,input1);
SubDiffFrame.showImage(img);
IplImage hsvImg = imgProc.rgbToHSV(input2);
hsv.showImage(hsvImg);
IplImage gray = imgProc.rgbToGrey(img);
grayscale.showImage(gray);
IplImage binaryImg = imgProc.getBinary(gray);
binary.showImage(binaryImg);
IplImage multiplyImage = imgProc.cvCopyImages(binaryImg, input2);
cvMultiplyAcc.showImage(multiplyImage);
IplImage hsvImg2 = imgProc.rgbToHSV(multiplyImage);
hsv2.showImage(hsvImg2);
CvHistogram hueHistogram = imgProc.getHueHistogram(hsvImg2);
System.out.println("Done");
System.out.printf("max hist value is: ", imgProc.getHistMaxValue(hueHistogram));
}
}
这里还有 cvCopyImage 方法:
public static IplImage cvCopyImages (IplImage binary, IplImage color){
IplImage result = cvCreateImage(new CvSize(color.width(),color.height()),color.depth(),3);
cvCopy(color,result,binary);
return result;
}