python opencv天空提取_python利用opencv实现印章的提取

这篇博客介绍了如何使用Python的OpenCV库进行天空和印章的图像提取。通过色彩阈值处理、膨胀操作以及轮廓查找等步骤,有效地分离出图像中的天空和印章部分,并最终进行裁剪和调整。
摘要由CSDN通过智能技术生成

抽空写了下,怕找不到了,做个备份,直接上代码,嗯,自己能看懂就喜欢这种感觉:

#coding:utf-8

import cv2

import numpy as np

#加载图片

image = cv2.imread('img\\111.jpg')

#统一处理图片大小

img_w = 650 if image.shape[1] > 600 else 400

image = cv2.resize(image, (img_w,int(img_w*image.shape[0]/image.shape[1])), interpolation=cv2.IMREAD_COLOR)

impng=cv2.cvtColor(image.copy(), cv2.COLOR_RGB2RGBA)

# image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)

#image = cv2.imread('img\\public-2004-16-a7912dbd-72b0-4d96-9d65-70abc21940d5.png')

hue_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

low_range = np.array([130, 43, 46])

high_range = np.array([180, 255, 255])

th = cv2.inRange(hue_image, low_range, high_range)

element = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))

th = cv2.dilate(th, element)

index1 = th == 255

img1 = np.zeros(impng.shape, np.uint8)

img1[:,:,:] = (255, 255, 255,0)

img1[index1] = impng[index1] # (0,0,255)

# img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)

# 创建四通道图片

#img1=topng(img1)

#img1 = cv2.cvtColor(img1, cv2.COLOR_RGB2RGBA)

cv2.imwrite('m1.png', img1)

cv2.imshow('1', img1)

#cv2.waitKey()

# cv2.imwrite("img1.png", img1)

low_range = np.array([0, 43, 46])

high_range = np.array([9, 255, 255])

th = cv2.inRange(hue_image, low_range, high_range)

element = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))

th = cv2.dilate(th, element)

index1 = th == 255

img2 = np.zeros(impng.shape, np.uint8)

img2[:,:,:] = (255, 255, 255,0)

img2[index1] = impng[index1]

cv2.imshow('2', img2)

cv2.imwrite('m2.png', img2)

imgreal=cv2.add(img2,img1)

cv2.imwrite('m222.png', imgreal)

#cv2.waitKey()

#img31 = cv2.cvtColor(img31, cv2.COLOR_BGR2RGB)

white_px = np.asarray([255, 255, 255,255])

#black_px = np.asarray([0, 0, 0, 255])

(row, col, _) = imgreal.shape

for r in range(row):

for c in range(col):

px = imgreal[r][c]

if all(px == white_px):

imgreal[r][c] = impng[r][c]

#扩充图片防止截取部分

img4png=cv2.copyMakeBorder(imgreal, 50, 50, 50, 50,cv2.BORDER_CONSTANT, value=[255, 255, 255,0])

img2gray = cv2.cvtColor(img4png, cv2.COLOR_RGBA2GRAY)

retval, grayfirst = cv2.threshold(img2gray, 254, 255, cv2.THRESH_BINARY_INV)

# 再次膨胀,轮廓查找

# img6 = cv2.GaussianBlur(grayfirst, (25, 5), 0, 0)

element = cv2.getStructuringElement(cv2.MORPH_RECT, (22, 22))

img6 = cv2.dilate(grayfirst, element)

#cv2.imshow('img4', img6)

c_canny_img = cv2.Canny(img6, 10, 10)

contours, hierarchy = cv2.findContours(c_canny_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

areas = []

for i, cnt in enumerate(contours):

rect = cv2.minAreaRect(cnt)

x, y, w, h = cv2.boundingRect(cnt)

area = w * h

ars = [area, i]

areas.append(ars)

areas = sorted(areas, reverse=True)

print(areas)

maxares = areas[:1]

x, y, w, h = cv2.boundingRect(contours[maxares[0][1]])

temp = img4png[y:(y + h), x:(x + w)]

#高小于宽

print(temp.shape)

if temp.shape[0]

#temp = cv2.resize(temp, (150, temp.shape[0]))

zh=int((temp.shape[1]-temp.shape[0])/2)

temp = cv2.copyMakeBorder(temp, zh, zh, 0, 0, cv2.BORDER_CONSTANT, value=[255, 255, 255,0])

else:

#temp = cv2.resize(temp, (temp.shape[1], 150))

zh = int((temp.shape[0] - temp.shape[1]) / 2)

temp = cv2.copyMakeBorder(temp, 0,0,zh, zh, cv2.BORDER_CONSTANT, value=[255, 255, 255,0])

dst = cv2.resize(temp, (150, 150))

cv2.imshow('3', dst)

cv2.imwrite('m0.png', dst)

cv2.waitKey()

看看效果:

![2020060820313558.jpg][]    ![20200608203221184.png][]

[2020060820313558.jpg]: /images/1603003438758.jpg

[20200608203221184.png]: /images/1603003390981.png

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCV中,去除红色印章通常涉及到颜色空间转换和图像处理技术,特别是使用色彩空间分离和边缘检测。以下是一个基本步骤的概述: 1. **色彩空间转换**:将图像从BGR(OpenCV默认)转换到HSV或HLS颜色空间,因为印章通常在特定的颜色范围内,如红色。 ```python import cv2 img = cv2.imread('image.jpg') hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) ``` 2. **设置红色阈值**:根据印章的颜色选择合适的HSV范围,比如红色印章大约在(0, 100, 100)至(10, 255, 255)之间。你可以使用`cv2.inRange()`函数创建一个红色掩码。 ```python lower_red = (0, 50, 50) upper_red = (10, 255, 255) mask = cv2.inRange(hsv_img, lower_red, upper_red) ``` 3. **腐蚀与膨胀**:为了消除边缘的噪声,可以对红色掩码应用腐蚀和膨胀操作。 ```python kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) ``` 4. **图像减法**:用原图像减去红色掩模,得到去除印章后的图像。 ```python result = cv2.bitwise_and(img, img, mask=mask) ``` 5. **可能的后期处理**:如果结果仍有残留,可以尝试使用形态学操作、边缘检测或者机器学习算法进一步优化。 ```python # 可能的后续操作... edges = cv2.Canny(result, threshold1, threshold2) ``` **相关问题--:** 1. OpenCV中的哪些函数常用于图像颜色空间转换? 2. 在去除印章过程中,为什么要进行腐蚀和膨胀操作? 3. 除了颜色阈值,还有哪些方法可以用来识别印章区域?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值