欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

如何通过javacv实现图片去水印(附代码)

时间:2020-11-20 17:40:21|栏目:JAVA代码|点击:

最近工作需求要做图片去水印的功能,研究了一段时间发现javacv能做这个功能,然后总结了一下。

首先导入maven

<dependency>
      <groupId>org.bytedeco</groupId>
      <artifactId>javacv</artifactId>
      <version>1.4.3</version>
      <exclusions>
        <exclusion>
          <groupId>org.bytedeco</groupId>
          <artifactId>javacpp</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>flycapture</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>libdc1394</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>libfreenect</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>libfreenect2</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>librealsense</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>videoinput</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>tesseract</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>leptonica</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>flandmark</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>artoolkitplus</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
<dependency>
      <groupId>org.bytedeco</groupId>
      <artifactId>javacv-platform</artifactId>
      <version>1.4.3</version>
      <exclusions>
        <exclusion>
          <groupId>org.bytedeco</groupId>
          <artifactId>javacv</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>flycapture-platform</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>libdc1394-platform</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>libfreenect-platform</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>libfreenect2-platform</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>librealsense-platform</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>videoinput-platform</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>tesseract-platform</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>leptonica-platform</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>flandmark-platform</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.bytedeco.javacpp-presets</groupId>
          <artifactId>artoolkitplus-platform</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.bytedeco.javacpp-presets</groupId>
      <artifactId>ffmpeg</artifactId>
      <version>4.0.2-1.4.3</version>
    </dependency>

开始进行实现:

第一步:

前景背景求差法

#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include "opencv2/photo/photo.hpp"
 
#include <iostream>
 
using namespace std;
using namespace cv;
 
int main(int argc, char** argv)
{
	cv::VideoCapture cap("C:/Users/zyy/Desktop/test_logs/test.mp4");
	if (!cap.isOpened())
	{
		cout << "Failed!" << endl;
		return -1;
	}
 
	Mat result, mask, dst1;
	Mat inpaintMask1;
	Mat inpaintMask = imread("C:/Users/zyy/Desktop/test_logs/log5.jpg");
	cv::cvtColor(inpaintMask, inpaintMask, CV_BGR2GRAY);
	cv::namedWindow("Result", 1);
	cv::namedWindow("Frame", 1);
	int i = 0;
	for (;;)
	{
		Mat frame;
		cap >> frame;
		if (!frame.data)
		{
			cout << "Over!" << endl;
			break;
		}
 
		if (i <= 2) //看视频,自己调试确定这个值,二十几帧之后左上角那个小图才出来,所以选了30
		{
			if (i == 2)
			{
				mask = frame;
				cv::cvtColor(mask, mask, CV_BGR2GRAY);
				//Mat inpaintMask1(mask.rows, mask.cols, CV_32FC1, 1);
				inpaintMask1 = mask;
			}
		}
		else
		{
			cv::bitwise_and(result, mask, mask);//<span style="font-size:18px;">前景背景求差法</span>
			if (i > 2 && i <= 40)
			{
				cv::cvtColor(frame, result, CV_BGR2GRAY);
				cv::bitwise_and(result, mask, mask);//<span style="font-size:18px;">前景背景求差法</span>
				inpaintMask1 = mask;
				imwrite("C:/Users/zyy/Desktop/test_logs/www.jpg", inpaintMask1);
			}
			Mat inpainted;
			inpaint(frame, inpaintMask, inpainted, 8, CV_INPAINT_TELEA);
			imshow("inpainted image", inpainted);
 
			imshow("Frame", frame);
			imshow("Result", mask);
			//waitKey();
			if (cv::waitKey(33) >= 0) break;
		}
		i++;
	}
 
	waitKey();
	return 0;
}

第二步:

logo和水印位置检测

#include <iostream>
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
using namespace std;
int main()
{
	IplImage *src = cvLoadImage("C:/Users/zyy/Desktop/test_logs/22.jpg", 0);
	IplImage *srcResult = cvLoadImage("C:/Users/zyy/Desktop/test_logs/22.jpg", 3); //用来显示
	IplImage *templat = cvLoadImage("C:/Users/zyy/Desktop/test_logs/yk2.jpg", 0);
	IplImage *result;
	if (!src || !templat)
	{
		cout << "打开图像失败" << endl;
		return 0;
	}
	int srcW, srcH, templatW, templatH, resultH, resultW;
	srcW = src->width;
	srcH = src->height;
	templatW = templat->width;
	templatH = templat->height;
	if (srcW < templatW || srcH < templatH)
	{
		cout << "模板不能比原图像小" << endl;
		return 0;
	}
	resultW = srcW - templatW + 1;
	resultH = srcH - templatH + 1;
	result = cvCreateImage(cvSize(resultW, resultH), 32, 1);
	cvMatchTemplate(src, templat, result, CV_TM_SQDIFF_NORMED);//CV_TM_SQDIFF_NORMED
	double minValue, maxValue;
	CvPoint minLoc, maxLoc;
	cvMinMaxLoc(result, &minValue, &maxValue, &minLoc, &maxLoc);
	cout << "最小值:" << minValue << " 最大值:" << maxValue << endl;
	cvRectangle(srcResult, minLoc, cvPoint(minLoc.x + templatW, minLoc.y + templatH), cvScalar(0, 0, 255));
	cvNamedWindow("srcResult", 0);
	cvNamedWindow("templat", 0);
	cvShowImage("srcResult", srcResult);
	cvShowImage("templat", templat);
	cvWaitKey(0);
	cvReleaseImage(&result);
	cvReleaseImage(&templat);
	cvReleaseImage(&srcResult);
	cvReleaseImage(&src);
	return 0;
}

第三步:

在已经求得掩膜图像的情况下,进行去logo操作。

  static String video_path="D://video//videos//";
  public static void ets( ) {
    Mat inpaintMask = new Mat();
    Mat inpainted=new Mat();
    Mat template = imread("D://video//movie_10.jpg");
    Mat frame = new Mat();
    cvtColor(template, inpaintMask, COLOR_BGR2GRAY);
    VideoCapture cap = new VideoCapture("D://video//mp4//1553583032966-480p.mp4");
    if(!cap.isOpened())
      return;
    String fileName=video_path+"VideoTest12.mp4";
    Size size = new Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH), (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
    double fps = cap.get(CV_CAP_PROP_FPS);
    VideoWriter vw1 = new VideoWriter(fileName, CV_FOURCC((byte)'D',(byte)'I',(byte)'V',(byte)'3'), fps/6, size, true);

    int start =11;
    int end =20;
    for (int i=start;i<end;i++){
      frame = imread("D://video//images//movie_"+i+".jpg");
      inpaint(frame, inpaintMask, inpainted, 8, INPAINT_TELEA);
//      opencv_imgcodecs.imwrite(video_path+"eguid"+i+".jpg", inpainted);
      vw1.write(inpainted); //写成视频文件
    }
    vw1.release();
  }

上述的图像和MP4文件自己准备就可以。感兴趣的可以去测试了。

上一篇:Spring Boot系列教程之7步集成RabbitMQ的方法

栏    目:JAVA代码

下一篇:深入理解hibernate的三种状态

本文标题:如何通过javacv实现图片去水印(附代码)

本文地址:http://www.codeinn.net/misctech/24409.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有