python识别图中是否包含另一张图片

安装虚拟环境

conda create -p E:\Python\envs\cvstu  python=3.8
conda activate E:\Python\envs\cvstu

安装所需要的包

pip install opencv-python
pip install matplotlib

编写代码

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 找图 返回最近似的点
def search_returnPoint(img, template, template_size):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    template_ = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
    result = cv2.matchTemplate(img_gray, template_, cv2.TM_CCOEFF_NORMED)
    threshold = 0.9
    # res大于70%
    loc = np.where(result >= threshold)
    # 使用灰度图像中的坐标对原始RGB图像进行标记
    point = ()
    for pt in zip(*loc[::-1]):
        cv2.rectangle(img, pt, (pt[0] + template_size[1], pt[1] + + template_size[0]), (7, 249, 151), 2)
        point = pt
    if point == ():
        return None, None, None
    return img, point[0] + template_size[1] / 2, point[1]

if __name__ == '__main__':
    scale = 1
    img = cv2.imread('./big.png')  # 要找的大图
    img = cv2.resize(img, (0, 0), fx=scale, fy=scale)

    template = cv2.imread('./small.png')  # 图中的小图
    template = cv2.resize(template, (0, 0), fx=scale, fy=scale)
    template_size = template.shape[:2]

    img, x_, y_ = search_returnPoint(img, template, template_size)
    if (img is None):
        print("没找到图片")
    else:
        print("找到图片 位置:" + str(x_) + " " + str(y_))
        plt.figure()
        plt.imshow(img, animated=True)
        plt.show()
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
识别一张图片有几种颜色,可以使用OpenCVPython。以下是一种实现方法: 1. 读取图像并将其转换为HSV颜色空间。 ```python import cv2 img = cv2.imread('image.jpg') hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) ``` 2. 定义颜色范围。在HSV颜色空间,每种颜色都有一个特定的H(色调),S(饱和度)和V(明度)值。通过调整这些值,可以定义特定颜色的范围。 ```python # 颜色范围 # 红色 lower_red = np.array([0, 100, 100]) upper_red = np.array([10, 255, 255]) lower_red2 = np.array([160, 100, 100]) upper_red2 = np.array([179, 255, 255]) # 绿色 lower_green = np.array([35, 43, 46]) upper_green = np.array([77, 255, 255]) # 蓝色 lower_blue = np.array([100, 43, 46]) upper_blue = np.array([124, 255, 255]) ``` 3. 使用cv2.inRange()函数将图像的像素值限制在指定的颜色范围内。这将创建一个蒙版,其包含指定颜色的像素为白色,其他像素为黑色。 ```python # 红色 mask_red = cv2.inRange(hsv_img, lower_red, upper_red) mask_red2 = cv2.inRange(hsv_img, lower_red2, upper_red2) mask_red = cv2.addWeighted(mask_red, 1.0, mask_red2, 1.0, 0.0) # 绿色 mask_green = cv2.inRange(hsv_img, lower_green, upper_green) # 蓝色 mask_blue = cv2.inRange(hsv_img, lower_blue, upper_blue) ``` 4. 对蒙版应用cv2.bitwise_or()函数,以便将所有颜色的像素组合到一个图像。 ```python # 通过位或操作将三个蒙版合并成一个图像 mask = cv2.bitwise_or(mask_red, mask_green, mask_blue) ``` 5. 使用cv2.findContours()函数查找图像的轮廓。在此步骤,可以通过指定轮廓面积的最小值来过滤掉不需要的轮廓。 ```python # 查找轮廓 contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 过滤掉面积较小的轮廓 min_area = 50 contours = [c for c in contours if cv2.contourArea(c) > min_area] ``` 6. 最后,可以通过len()函数计算检测到的颜色数量。 ```python # 计算颜色数量 num_colors = len(contours) print(f'图像检测到了{num_colors}种颜色。') ``` 这就是用OpenCVPython识别一张图片有几种颜色的方法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值