图像混合处理
h ( x ) = ( 1 − α ) f ( x ) + α g ( x ) α ∈ ( 0 , 1 ) h\left( x \right) =\left( 1-\alpha \right) f\left( x \right) +\alpha g\left( x \right) \,\, \alpha \in \left( 0,1 \right) h(x)=(1−α)f(x)+αg(x)α∈(0,1)
代码1:
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.Size; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; //图像混合 public class ImageMixture { static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); } public static void main(String[] args) { addWeightedFunction(); } public static void addWeightedFunction() { Mat src1= Imgcodecs.imread("D:\\Eclipse\\Opencv\\pictures\\triangle.png"); Mat src2=Imgcodecs.imread("D:\\Eclipse\\Opencv\\pictures\\circle.png"); Mat image=new Mat(); double alpha=0.5; Core.addWeighted(src1,1-alpha,src2,alpha,0.0,image); HighGui.imshow("图像混合", image); HighGui.waitKey(0); } }
运行结果:
原因:
两张图片的像素大小不一致,而 C o r e . a d d W e i g h t e d ( ) Core.addWeighted() Core.addWeighted() 方法要求参与混合的图片尺寸要完全一致。
代码2:
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.Size; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; //图像混合 public class ImageMixture { static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); } public static void main(String[] args) { addWeightedFunction(); } public static void addWeightedFunction() { Mat src1= Imgcodecs.imread("D:\\Eclipse\\Opencv\\pictures\\triangle.png"); Mat src2=Imgcodecs.imread("D:\\Eclipse\\Opencv\\pictures\\circle.png"); Imgproc.resize(src1, src1, new Size(300,291)); Imgproc.resize(src2, src2, new Size(300,291)); Mat image=new Mat(); double alpha=0.5; Core.addWeighted(src1,1-alpha,src2,alpha,0.0,image); HighGui.imshow("图像混合", image); HighGui.waitKey(0); } }
原图:
运行结果: