图像处理是一门多学科的领域,涉及数学、计算机科学和工程学等多个领域的知识。它可以应用于各种领域,包括医学影像处理、计算机视觉、图像识别和增强现实等。在这篇文章中,我们将探索如何使用Java中的OpenCV库来进行图像操作,以及如何利用它的功能来创建令人印象深刻的图像处理应用。
2025年01月27日
图像处理是一门多学科的领域,涉及数学、计算机科学和工程学等多个领域的知识。它可以应用于各种领域,包括医学影像处理、计算机视觉、图像识别和增强现实等。在这篇文章中,我们将探索如何使用Java中的OpenCV库来进行图像操作,以及如何利用它的功能来创建令人印象深刻的图像处理应用。
2025年01月27日
今天,我们想与您分享解决图像偏移校正问题(拉直旋转图像)的简单解决方案。如果我们正在从歪斜图像中提取文本的内容,则必须以一种或另一种形式处理图像。从摄像机图片到扫描的文档-将清理后的图像馈送到OCR工具之前,去歪斜是图像预处理中的必要步骤。
2025年01月27日
主要介绍通过
https://github.com/opencv/opencv/releases 下载的 SDK的目录结构。
2025年01月27日
接上篇文章, 我做了透视矫正的功能,且实现裁剪,但是效果不是很理想,欢迎小伙伴来评论
import cv2
import numpy as np
def process_image(image, is_original=False):
# 1. 复制图层并进行高斯模糊
blurred = cv2.GaussianBlur(image, (201, 201), 0).astype(float)
# 2. 实现“划分”模式
epsilon = 1e-7
divided = image / (blurred + epsilon)
# 将结果缩放到0-255范围并转换为8位无符号整数
divided = np.clip(divided * 255, 0, 255).astype(np.uint8)
merged = divided.astype(float) # 转换为浮点数以避免操作中的整数截断
# 3. 实现正片叠底模式
multiply = (divided * merged) / 255
return np.clip(multiply, 0, 255).astype(np.uint8)
def scan_effect(image_path):
# 读取原始图像
original = cv2.imread(image_path)
gray = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
# 边缘检测
edged = cv2.Canny(gray, 50, 150)
# 膨胀操作,增强轮廓
dilated = cv2.dilate(edged, None, iterations=2)
# 找到轮廓
contours, _ = cv2.findContours(dilated, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
# 遍历轮廓,找到大概是文档的四边形
screen_contour = None
for contour in contours:
peri = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, 0.01 * peri, True)
if len(approx) == 4:
screen_contour = approx
break
if screen_contour is not None:
# 在原始图像上绘制轮廓
# cv2.drawContours(original, [screen_contour], -1, (0, 255, 0), 2)
# 透视变换
def order_points(pts):
if len(pts.shape) == 3:
pts = pts.reshape(4, 2)
rect = np.zeros((4, 2), dtype="float32")
center = np.mean(pts, axis=0)
for point in pts:
if point[0] < center[0] and point[1] < center[1]:
rect[0] = point # 左上
elif point[0] > center[0] and point[1] < center[1]:
rect[1] = point # 右上
elif point[0] > center[0] and point[1] > center[1]:
rect[2] = point # 右下
else:
rect[3] = point # 左下
return rect
def four_point_transform(image, pts):
rect = order_points(pts)
(tl, tr, br, bl) = rect
widthA = np.linalg.norm(br - bl)
widthB = np.linalg.norm(tr - tl)
maxWidth = max(int(widthA), int(widthB))
heightA = np.linalg.norm(tr - br)
heightB = np.linalg.norm(tl - bl)
maxHeight = max(int(heightA), int(heightB))
dst = np.array([[0, 0], [maxWidth - 1, 0], [maxWidth - 1, maxHeight - 1], [0, maxHeight - 1]], dtype="float32")
M = cv2.getPerspectiveTransform(rect, dst)
return cv2.warpPerspective(image, M, (maxWidth, maxHeight))
warped = four_point_transform(original, screen_contour.reshape(4, 2))
multiply = process_image(warped)
else:
multiply = process_image(original, is_original=True)
# 显示和保存最终结果
cv2.imshow("Result", multiply)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite(r'C:\Users\40650\Desktop\20241009171353-1.jpg', multiply)
scan_effect(r'C:\Users\40650\Desktop\20241009171353.jpg')
2025年01月27日
前几天我说熟悉OpenCV一周或两周就差不多了。今天我就详细分析下,为什么可以短时间掌握opencv。我主要从以下几个方面进行分析:
1、计算机视觉能干什么?
2、计算机视觉算法分类。
2025年01月27日
python 版本:Python 3
目前市面上有很多类似的工具,比如夸克的图片文档处理,效果较好,可以实现透视矫正。
之前在处理孩子老师发布的作业,发现照片很清晰,打印会有灰色的背景,是因为拍照的光线问题导致的。
2025年01月27日
OpenCV是一个(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows和Mac OS操作系统上。它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。
在学习过程中遇到问题最好的办法就是查询opencv官方文档。