基于相似度对不同情况进行分类

145次阅读
没有评论

共计 2491 个字符,预计需要花费 7 分钟才能阅读完成。

提醒:本文最后更新于 2024-08-30 15:22,文中所关联的信息可能已发生改变,请知悉!

概述

graph 
  A(取 3 * 3 的像素点) -->B(视为 2 * 2 相邻格子)
 B --> C(获得 2 * 2 区分度 S)
 C -->| 存在阈值 D > S| D(标记为 0)
 C -->| 存在阈值 D  F(对 01 位置进行分类)
 E --> F
 F -->|3:1| G(标为蓝色)
 F -->|2:2 相邻 | H(标为绿色)
 F -->|2:2 对角 | I(标为红色)

三种情况分开处理

# python3.8
# utf-8

import cv2 as cv
import numpy as np

def get_qufendu(array):
    qufendu = np.zeros((2, 2))
    result = np.zeros((2, 2))
    for i in range(2):
        for j in range(2):
            qufendu[i][j] = max(array[i][j], array[i+1][j], array[i+1][j+1], array[i][j+1]) - min(array[i][j], array[i+1][j], array[i+1][j+1], array[i][j+1])
            if qufendu[i][j] > 40:
                result[i][j] = 1
    return result

def classify_array(array):
    a_sum = array[0][0] + array[0][1] + array[1][1] + array[1][0]
    if a_sum == 0 or a_sum == 4:
        return 0
    elif a_sum == 1 or a_sum == 3:
        return 1
    elif a_sum == 2:
        if array[0][0] == array[0][1] or array[0][0] == array[1][0]:
            return 2
        else:
            return 3

def get_channel_flag(channel, channel_flag, k):
    row, col, cnt_channel = channel.shape
    for i in range(row-2):
        for j in range(col-2):
            array33 = np.zeros((3, 3))
            for x in range(3):
                for y in range(3):
                    array33[x][y] = channel[i+x][j+y][k]
            array_flag = get_qufendu(array33)
            channel_flag[i+1][j+1][k] = classify_array(array_flag)
    return channel, channel_flag

def get_flag(img):
    flag = np.zeros(img.shape)
    row, col, cnt_channel = img.shape
    for k in range(cnt_channel):
        img, flag = get_channel_flag(img, flag, k)
    return flag

def get_value(pixel, flag):
    if flag == 1:
        pixel[0] = 255
        pixel[1] = 0
        pixel[2] = 0
    elif flag == 2:
        pixel[0] = 0
        pixel[1] = 255
        pixel[2] = 0
    elif flag == 3:
        pixel[0] = 0
        pixel[1] = 0
        pixel[2] = 255
    else:
        pixel[0] = 255
        pixel[1] = 255
        pixel[2] = 255
    return pixel

def mark(img, flag):
    row, col, cnt_channel = img.shape
    for i in range(row):
        for j in range(col):
            if flag[i][j][0] == flag[i][j][1] and flag[i][j][1] == flag[i][j][2]:
                img[i][j] = get_value(img[i][j], flag[i][j][0])
    return img

def img2three(img):
    row, col, channel_cnt = img.shape
    img1 = np.zeros((row, col, channel_cnt))
    img2 = np.zeros((row, col, channel_cnt))
    img3 = np.zeros((row, col, channel_cnt))

    for i in range(row):
        for j in range(col):
            img1[i][j] = [255, 255, 255]
            img2[i][j] = [255, 255, 255]
            img3[i][j] = [255, 255, 255]

    for i in range(row):
        for j in range(col):
            if img[i][j][0] == 255 and img[i][j][1] == 0 and img[i][j][2] == 0:
                img1[i][j] = img[i][j]
            elif img[i][j][0] == 0 and img[i][j][1] == 255 and img[i][j][2] == 0:
                img2[i][j] = img[i][j]
            elif img[i][j][0] == 0 and img[i][j][1] == 0 and img[i][j][2] == 255:
                img3[i][j] = img[i][j]
            else:
                pass

    return img1, img2, img3

def main(img):
    # print(np.array([255, 0, 0]).any())
    p = cv.imread(img, 1)
    # b, g, r = cv.split(p)
    flag = get_flag(p)
    p = mark(p, flag)
    cv.imwrite('mark_' + img, p)
    img1, img2, img3 = img2three(p)
    cv.imwrite('mark1_' + img, img1)
    cv.imwrite('mark2_' + img, img2)
    cv.imwrite('mark3_' + img, img3)

if __name__ == '__main__':
    img_addr = '8068.jpg'
    main(img_addr)

基于相似度对不同情况进行分类

基于相似度对不同情况进行分类

基于相似度对不同情况进行分类

基于相似度对不同情况进行分类

正文完
 0
icvuln
版权声明:本站原创文章,由 icvuln 于2022-05-30发表,共计2491字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)