1.下载
官网下载地址:https://www.python.org/downloads/
python安装包镜像下载地址: https://registry.npmmirror.com/binary.html?path=python/
2.安装

image.png

image.png

image.png
3.验证安装

image.png
如果运行python命令后没有正确输出,设置下对应python版本的环境变量,将python安装的目录和目录下的 Scripts 目录添加到 Path 环境变量中去

image.png
4.设置pip镜像
打开我的电脑或者随便打开一个文件夹,在资源管理器的地址栏输入%appdata%后回车

image.png

image.png
在打开的文件夹中下新建一个pip文件夹,进入pip文件夹中,新建一个 pip.ini 文件,输入如下内容
[global]
index-url = https://pypi.douban.com/simple
或者通过命令生成
配置pip镜像
python -m pip install --upgrade pip
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip config set global.extra-index-url "http://mirrors.aliyun.com/pypi/simple/
http://pypi.hustunique.com/ https://pypi.mirrors.ustc.edu.cn/simple/"
安装图像处理相关模块
pip install NumPy SciPy opencv-python matplotlib imutils
5.测试demo,测试cv是否能正常运行,新建文件test.py,内容如下。在当前文件下执行python test.py
import cv2 as cv
print(cv.getVersionString())
image = cv.imread("1.jpg")
print(image.shape)
cv.imshow("image",image)
cv.waitKey()
6java程序中调用示例
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.crfsdi.prospectai.bean.response.rockcore.DrillImgCombineDto;
import com.crfsdi.prospectai.service.DrillImgCombineService;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@Service
public class DrillImgConbineServideImpl implements DrillImgCombineService {
@Override
public String Combine(DrillImgCombineDto requestDto) {
StringBuilder ret;
ret = new StringBuilder();
try {
// 下载原文件
String filePath = "https://******/2683_1627726752632.jpg";
//HttpUtil.downloadFile(filePath, "C:\\Users\\JH\\Desktop\\123\\dist");
HttpUtil.downloadFile(filePath, "/");
Process proc;
String[] cmdArr = new String[]{"D:\\python\\python.exe",
"./DrillImgCombine.py",
Base64.encodeBase64String(Base64.encodeBase64String(JSONUtil.toJsonStr(requestDto).getBytes("utf-8")).getBytes("utf-8"))};
proc = Runtime.getRuntime().exec(cmdArr);// 执行py文件
// 用输入输出流来截取结果
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String error;
while ((error = stdError.readLine()) != null) {
System.err.println("=====+++++"+error);
}
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"gbk"));
String line = null;
while ((line = in.readLine()) != null) {
ret.append(line);
System.out.println("----+++++"+line);
}
in.close();
proc.waitFor();
// TODO 上传拼接处理文件。
// TODO 保存入库
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return ret.toString();
}
}
# -*- coding: UTF-8 -*-
import os,sys
import base64
import json
import cv2 as cv
import numpy as np
#import scipy.spatial
from imutils import perspective
imgPath = "D:\\python\\pyTest\\"
print(sys.argv[1])
jsonObj = base64.b64decode(base64.b64decode(sys.argv[1]))
print(jsonObj)
parms = json.loads(jsonObj)
# load the notecard code image, clone it, and initializeNo module named
#scipy.spatial the 4 points
# that correspond to the 4 corners of the notecard
notecard = cv.imread(imgPath + "1.jpg")
clone = notecard.copy()
pts = np.array(parms['pts'])
# loop over the points and draw them on the cloned image
for (x, y) in pts:
cv.circle(clone, (x, y), 5, (0, 255, 0), -1)
# apply the four point tranform to obtain a "birds eye view" of
# the notecard
warped = perspective.four_point_transform(notecard, pts)
# show the original and warped images
cv.namedWindow('Original',cv.WINDOW_NORMAL)
cv.imshow("Original", clone)
cv.namedWindow('Warped',cv.WINDOW_NORMAL)
cv.imshow("Warped", warped)
cv.imwrite(imgPath + "2.jpg",warped)
#height, width, channels = warped.shape
height=2000
width=int(height/2)
img2= cv.resize(warped,(height,width))
cv.imshow("img2", img2)
cv.imwrite(imgPath + "3.jpg",img2)
img3=img2[0:int(width/5),:]
img4=img2[int(width/5):int(2*width/5),:]
img5=img2[int(2*width/5):int(3*width/5),:]
img6=img2[int(3*width/5):int(4*width/5),:]
img7=img2[int(4*width/5):width,:]
cv.imshow("img3", img3)
cv.imshow("img4", img4)
cv.imshow("img5", img5)
cv.imshow("img6", img6)
cv.imshow("img7", img7)
inputs = np.hstack((img3,img4,img5,img6,img7))
cv.imshow("inputs", inputs)
cv.imwrite(imgPath + "4.jpg",inputs)
cv.waitKey(0)