最近工作需求要做圖片去水印的功能,研究了一段時(shí)間發(fā)現(xiàn)javacv能做這個(gè)功能,然后總結(jié)了一下。
公司主營(yíng)業(yè)務(wù):成都網(wǎng)站建設(shè)、網(wǎng)站制作、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)推出嘉黎免費(fèi)做網(wǎng)站回饋大家。
首先導(dǎo)入maven
org.bytedeco javacv 1.4.3 org.bytedeco javacpp org.bytedeco.javacpp-presets flycapture org.bytedeco.javacpp-presets libdc1394 org.bytedeco.javacpp-presets libfreenect org.bytedeco.javacpp-presets libfreenect2 org.bytedeco.javacpp-presets librealsense org.bytedeco.javacpp-presets videoinput org.bytedeco.javacpp-presets tesseract org.bytedeco.javacpp-presets leptonica org.bytedeco.javacpp-presets flandmark org.bytedeco.javacpp-presets artoolkitplus org.bytedeco javacv-platform 1.4.3 org.bytedeco javacv org.bytedeco.javacpp-presets flycapture-platform org.bytedeco.javacpp-presets libdc1394-platform org.bytedeco.javacpp-presets libfreenect-platform org.bytedeco.javacpp-presets libfreenect2-platform org.bytedeco.javacpp-presets librealsense-platform org.bytedeco.javacpp-presets videoinput-platform org.bytedeco.javacpp-presets tesseract-platform org.bytedeco.javacpp-presets leptonica-platform org.bytedeco.javacpp-presets flandmark-platform org.bytedeco.javacpp-presets artoolkitplus-platform org.bytedeco.javacpp-presets ffmpeg 4.0.2-1.4.3
開(kāi)始進(jìn)行實(shí)現(xiàn):
第一步:
前景背景求差法
#include#include #include #include "opencv2/photo/photo.hpp" #include 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) //看視頻,自己調(diào)試確定這個(gè)值,二十幾幀之后左上角那個(gè)小圖才出來(lái),所以選了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);//前景背景求差法 if (i > 2 && i <= 40) { cv::cvtColor(frame, result, CV_BGR2GRAY); cv::bitwise_and(result, mask, mask);//前景背景求差法 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和水印位置檢測(cè)
#include#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); //用來(lái)顯示 IplImage *templat = cvLoadImage("C:/Users/zyy/Desktop/test_logs/yk2.jpg", 0); IplImage *result; if (!src || !templat) { cout << "打開(kāi)圖像失敗" << 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; }
第三步:
在已經(jīng)求得掩膜圖像的情況下,進(jìn)行去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
上述的圖像和MP4文件自己準(zhǔn)備就可以。感興趣的可以去測(cè)試了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。