我试图将二进制图像掩盖到原始图像上,大部分时间它都有效,但我偶尔会得到这个:
关于为什么会发生这种情况以及如何解决问题的任何想法?在此先感谢:)
编辑:好的,所以我有两张图片,我发现它们之间的区别,最终成为了手。
良好结果的例子
然后我将差异图像转换为灰度,将图像生成图像(使用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;
}