gpt4 book ai didi

opencv - [OpenCV]如何将轮廓固定为矩形?

转载 作者:行者123 更新时间:2023-12-02 17:58:09 26 4
gpt4 key购买 nike

笔记
我是 OpenCV(或计算机视觉)的新手,所以告诉我搜索查询会很有帮助!
我想问什么
我想编写一个从图片中提取名片的程序。
我能够提取粗略的轮廓,但反射光会变成噪点,我无法提取准确的轮廓。请告诉我你的想法。
图像(原始数据)
raw data
输出
output data(rough outline)
代码

import math
import itertools
from glob import glob

import cv2
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

%matplotlib inline

def read_images():
"read image data from data directory"
names = glob('data/*.jpg')
names.sort()
return map(lambda name: cv2.imread(name), names)

def blur(img):
"apply blur"
return cv2.GaussianBlur(img, (25, 25), 0)

def show_images(images, column, color_type=cv2.COLOR_BGR2RGB):
"plot images with matplotlib"
plt.figure(figsize=(10,10), dpi=150)
for n, img in zip(range(len(images)), images):
p = plt.subplot(math.ceil(len(images) / column), column, n + 1)
p.axis('off')
if color_type is None:
p.imshow(img)
else:
p.imshow(cv2.cvtColor(img, color_type))
plt.show()

def detect_background_color(img):
"detect background color"
# Assume that the perimeter is all background
height, width, *_ = img.shape
background_colors = np.concatenate([
img[5:height-5, 5], img[5, 5:width-5],
img[5:height-5, width-5], img[height-5, 5:width-5]
])
background_colors = background_colors.astype(np.float32)

# Assume that the background color is only one.
K = 2
iter_flg = cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER
_, labels, centers = cv2.kmeans(
background_colors, K, None, (iter_flg, 10, 1.0), 10,
cv2.KMEANS_RANDOM_CENTERS)

cnt1 = len(labels[labels==0])
cnt2 = len(labels[labels==1])
return centers[0] if cnt1 > cnt2 else centers[1]

def scale(img):
bg = detect_background_color(img)
return np.fix(np.sqrt(np.sum(np.square(img - bg), axis=2)) / 1.732).astype(np.uint8)

def binarize(img):
th, bit = cv2.threshold(img, 40, 255, cv2.THRESH_BINARY)
return bit

binarized = [binarize(scale(blur(img))) for img in read_images()]
show_images(binarized, 4, None)

最佳答案

看起来你需要应用形态尝试 cv2.erode 然后 cv2 dilate 操作。
第一个将删除小于腐 eclipse 内核大小的区域,第二个将恢复大 blob 的初始大小。您需要为这两个操作应用相同大小的内核。
morphology .
还要检查:medium article

关于opencv - [OpenCV]如何将轮廓固定为矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64259055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com