opencv图像处理_python(一)

一、RGB图转换为GARY图

import cv2

img = cv2.imread(r'path\\img.png')    #打开图片
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)    #转成gary图片

二、遍历gary图片,做阈值处理

y = img.shape[1]
x = img.shape[0]

            # 遍历灰度图,阈值大于1的全变白
for i in range(x):
    for j in range(y):
        if gray_img[i, j] > 1:
            gray_img[i, j] = 255
        else:
            gray_img[i, j] = 0       

三、将图片在label组件中显示

如果直接使用

#错误写法
img2 = cv2.imread(r'path\\img.png')
self.label.setPixmap(img2)

此方法将会报错:

TypeError: setPixmap(self, QPixmap): argument 1 has unexpected type 'numpy.ndarray'

应该用如下写法:

global imgNamepath  # 这里为了方便别的地方引用图片路径,将其设置为全局变量

imgNamepath = ("path\\img.png")

# 通过文件路径获取图片文件,并设置图片长宽为label控件的长、宽
img = QtGui.QPixmap(imgNamepath).scaled(self.label_3_1.width(), self.label_3_1.height())

# 在label控件上显示选择的图片
self.label.setPixmap(img)

四、遍历二值图,获取指定颜色的pixel数量

black = 0
white = 0
# 遍历二值图,为0则black+1,否则white+1
for i in range(x):
    for j in range(y):
        if gray_img[i, j] == 0:
            black += 1
        else:
            white += 1
print("白色个数:", white)
print("黑色个数:", black)
rate1 = white / (x * y)
rate2 = black / (x * y)
# round()第二个值为保留几位有效小数。
str_1 = str(round(rate1 * 100, 2)) + '%'


print("白色面积所占整张图的百分比:",str_1)

print("白色占比:", round(rate1 * 100, 2), '%')
print("黑色占比:", round(rate2 * 100, 2), '%')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图像轮廓识别是一种常见的图像处理技术,可以用于物体检测、边缘检测、模式识别等领域。Python中的OpenCV库提供了丰富的图像处理功能,包括图像轮廓识别功能。下面介绍如何使用Python Opencv实现图像轮廓识别功能。 1. 导入库 首先需要导入OpenCV库。可以使用以下代码导入: ```python import cv2 import numpy as np ``` 2. 读取图像并转化为灰度图像 使用cv2.imread()函数读取图像,并使用cv2.cvtColor()函数将图像转化为灰度图像。代码如下: ```python img = cv2.imread("image.jpg") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ``` 3. 进行图像处理 在进行轮廓识别前,需要对图像进行一些处理,如图像平滑、二值化等。这里使用cv2.GaussianBlur()函数进行高斯平滑处理,并使用cv2.threshold()函数进行二值化处理。代码如下: ```python blur = cv2.GaussianBlur(gray, (5, 5), 0) ret, binary = cv2.threshold(blur, 127, 255, cv2.THRESH_BINARY) ``` 4. 进行轮廓识别 使用cv2.findContours()函数进行轮廓识别。该函数返回一个轮廓列表和一个层次结构。代码如下: ```python contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) ``` 5. 绘制轮廓 使用cv2.drawContours()函数绘制轮廓。代码如下: ```python cv2.drawContours(img, contours, -1, (0, 255, 0), 2) ``` 6. 显示图像 使用cv2.imshow()函数显示图像,并使用cv2.waitKey()函数等待按键。代码如下: ```python cv2.imshow("image", img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 完整代码如下: ```python import cv2 import numpy as np img = cv2.imread("image.jpg") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (5, 5), 0) ret, binary = cv2.threshold(blur, 127, 255, cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(img, contours, -1, (0, 255, 0), 2) cv2.imshow("image", img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 运行代码后,将显示原图像和识别出的轮廓。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值