久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 OpenCV 從桌面游戲卡圖像中提取藝術品

Extract artwork from table game card image with OpenCV(使用 OpenCV 從桌面游戲卡圖像中提取藝術品)
本文介紹了使用 OpenCV 從桌面游戲卡圖像中提取藝術品的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 python 中編寫了一個小腳本,我試圖提取或裁剪撲克牌中僅代表藝術品的部分,刪除所有其余部分.我一直在嘗試各種閾值方法,但無法實現.另請注意,我不能簡單地手動記錄藝術品的位置,因為它并不總是處于相同的位置或大小,而是總是呈矩形,其他一切都只是文本和邊框.

from matplotlib import pyplot as plt導入簡歷2img = cv2.imread(文件名)灰色 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)ret,binary = cv2.threshold(灰色, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY)二進制 = cv2.bitwise_not(二進制)內核 = np.ones((15, 15), np.uint8)關閉 = cv2.morphologyEx(二進制,cv2.MORPH_OPEN,內核)plt.imshow(關閉),plt.show()

當前輸出是我能得到的最接近的東西.我可能走在正確的道路上,并嘗試進一步爭論在白色部分周圍畫一個矩形,但我認為這不是一種可持續的方法:

最后一點,請參閱下面的卡片,并非所有框架的尺寸或位置都完全相同,但總有一件藝術品只有文字和邊框.它不必被非常精確地切割,但很明顯,藝術是卡片的一個區域",被包含一些文本的其他區域包圍.我的目標是盡可能地捕捉藝術品的區域.

解決方案

我使用霍夫線變換來檢測圖像的線性部分.所有線的交叉點用于構建所有可能的矩形,其中不包含其他交叉點.由于您要查找的卡片部分始終是這些矩形中最大的部分(至少在您提供的示例中),因此我只是選擇了這些矩形中最大的部分作為獲勝者.該腳本無需用戶交互即可運行.

導入 cv2將 numpy 導入為 np從集合導入 defaultdictdef segment_by_angle_kmeans(lines, k=2, **kwargs):#使用k-means根據角度對線進行分組.#在單位圓上的角度坐標上使用k-means#to 分割 `lines` 內的 `k` 角度.# 定義標準 = (type, max_iter, epsilon)default_criteria_type = cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER標準 = kwargs.get('標準', (default_criteria_type, 10, 1.0))flags = kwargs.get('flags', cv2.KMEANS_RANDOM_CENTERS)嘗試 = kwargs.get('嘗試', 10)# 以弧度返回 [0, pi] 中的角度角度 = np.array([line[0][1] for line in lines])# 將角度乘以 2 并找到該角度的坐標pts = np.array([[np.cos(2*angle), np.sin(2*angle)]對于角度角度],dtype=np.float32)# 在坐標上運行 kmeans標簽,中心= cv2.kmeans(pts,k,無,標準,嘗試,標志)[1:]labels = labels.reshape(-1) # 轉置為行 vec# 根據 kmeans 標簽分割線分段 = 默認字典(列表)對于 i,zip 中的行(范圍(len(行)),行):分段[labels[i]].append(line)分段 = 列表(分段.值())分段返回def 交叉點(第 1 行,第 2 行):#找到以Hesse范式給出的兩條線的交點.#返回最近的整數像素位置.#參見 https://stackoverflow.com/a/383527/5087436rho1, theta1 = line1[0]rho2, theta2 = line2[0]A = np.array([[np.cos(theta1), np.sin(theta1)],[np.cos(theta2), np.sin(theta2)]])b = np.array([[rho1], [rho2]])x0, y0 = np.linalg.solve(A, b)x0, y0 = int(np.round(x0)), int(np.round(y0))返回 [[x0, y0]]def segmented_intersections(線):#查找線組之間的交點.交叉點 = []對于我,在枚舉中分組(行 [:-1]):對于行 [i+1:] 中的 next_group:對于組中的第 1 行:對于 next_group 中的 line2:intersections.append(intersection(line1, line2))返回路口def rect_from_crossings(crossings):#查找內部沒有其他點的所有矩形矩形 = []# 搜索所有可能的矩形對于我在范圍內(len(crossings)):x1= int(交叉點[i][0][0])y1= int(交叉點[i][0][1])對于范圍內的 j (len(crossings)):x2= int(交叉點[j][0][0])y2= int(交叉點[j][0][1])#搜索所有點標志 = 1對于范圍內的k(len(crossings)):x3= int(過境點[k][0][0])y3= int(交叉點[k][0][1])#Dont count double (反向矩形)如果(x1 > x2 或 y1 > y2):標志 = 0#不要計算里面有點的矩形elif ((((x3 >= x1) and (x2 >= x3))and (y3 > y1) and (y2 > y3) or ((x3 > x1) and (x2 > x3))和 (y3 >= y1) 和 (y2 >= y3))):如果(i!=k 和 j!=k):標志 = 0如果標志:矩形.append([[x1,y1],[x2,y2]])返回矩形如果 __name__ == '__main__':#img = cv2.imread('TAJFp.jpg')#img = cv2.imread('Bj2uu.jpg')img = cv2.imread('yi8db.png')寬度 = int(img.shape[1])高度 = int(img.shape[0])比例 = 380/寬度暗淡=(int(寬度*比例),int(高度*比例))# 調整圖片大小img = cv2.resize(img,暗淡,插值 = cv2.INTER_AREA)img2 = img.copy()灰色 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)灰色 = cv2.GaussianBlur(灰色,(5,5),cv2.BORDER_DEFAULT)# Canny 和 Hough 的參數可能需要調整以適用于盡可能多的卡片邊緣 = cv2.Canny(灰色,10,45,孔徑 = 7)線 = cv2.HoughLines(edges,1,np.pi/90,160)分段 = segment_by_angle_kmeans(線)交叉口 = 分段交點(分段)矩形 = rect_from_crossings(crossings)#找到最大的剩余矩形大小 = 0對于我在范圍內(len(矩形)):x1 = 矩形[i][0][0]x2 = 矩形[i][1][0]y1 = 矩形[i][0][1]y2 = 矩形[i][1][1]如果(尺寸 < (abs(x1-x2)*abs(y1-y2))):大小 = abs(x1-x2)*abs(y1-y2)x1_rect = x1x2_rect = x2y1_rect = y1y2_rect = y2cv2.rectangle(img2, (x1_rect,y1_rect), (x2_rect,y2_rect), (0,0,255), 2)投資回報率 = img[y1_rect:y2_rect, x1_rect:x2_rect]cv2.imshow("輸出",roi)cv2.imwrite("輸出.png", roi)cv2.waitKey()

這些是您提供的樣本的結果:

查找線交叉的代碼可以在這里找到:查找使用 houghlines opencv 繪制的兩條線的交點

您可以在這里閱讀更多關于霍夫線的信息..p>

I wrote a small script in python where I'm trying to extract or crop the part of the playing card that represents the artwork only, removing all the rest. I've been trying various methods of thresholding but couldn't get there. Also note that I can't simply record manually the position of the artwork because it's not always in the same position or size, but always in a rectangular shape where everything else is just text and borders.

from matplotlib import pyplot as plt
import cv2

img = cv2.imread(filename)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

ret,binary = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY)

binary = cv2.bitwise_not(binary)
kernel = np.ones((15, 15), np.uint8)

closing = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)

plt.imshow(closing),plt.show()

The current output is the closest thing I could get. I could be on the right way and try some further wrangling to draw a rectangle around the white parts, but I don't think it's a sustainable method :

As a last note, see the cards below, not all frames are exactly the same sizes or positions, but there's always a piece of artwork with only text and borders around it. It doesn't have to be super precisely cut, but clearly the art is a "region" of the card, surrounded by other regions containing some text. My goal is to try to capture the region of the artwork as well as I can.

解決方案

I used Hough line transform to detect linear parts of the image. The crossings of all lines were used to construct all possible rectangles, which do not contain other crossing points. Since the part of the card you are looking for is always the biggest of those rectangles (at least in the samples you provided), i simply chose the biggest of those rectangles as winner. The script works without user interaction.

import cv2
import numpy as np
from collections import defaultdict

def segment_by_angle_kmeans(lines, k=2, **kwargs):
    #Groups lines based on angle with k-means.
    #Uses k-means on the coordinates of the angle on the unit circle 
    #to segment `k` angles inside `lines`.

    # Define criteria = (type, max_iter, epsilon)
    default_criteria_type = cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER
    criteria = kwargs.get('criteria', (default_criteria_type, 10, 1.0))
    flags = kwargs.get('flags', cv2.KMEANS_RANDOM_CENTERS)
    attempts = kwargs.get('attempts', 10)

    # returns angles in [0, pi] in radians
    angles = np.array([line[0][1] for line in lines])
    # multiply the angles by two and find coordinates of that angle
    pts = np.array([[np.cos(2*angle), np.sin(2*angle)]
                    for angle in angles], dtype=np.float32)

    # run kmeans on the coords
    labels, centers = cv2.kmeans(pts, k, None, criteria, attempts, flags)[1:]
    labels = labels.reshape(-1)  # transpose to row vec

    # segment lines based on their kmeans label
    segmented = defaultdict(list)
    for i, line in zip(range(len(lines)), lines):
        segmented[labels[i]].append(line)
    segmented = list(segmented.values())
    return segmented

def intersection(line1, line2):
    #Finds the intersection of two lines given in Hesse normal form.
    #Returns closest integer pixel locations.
    #See https://stackoverflow.com/a/383527/5087436

    rho1, theta1 = line1[0]
    rho2, theta2 = line2[0]

    A = np.array([
        [np.cos(theta1), np.sin(theta1)],
        [np.cos(theta2), np.sin(theta2)]
    ])
    b = np.array([[rho1], [rho2]])
    x0, y0 = np.linalg.solve(A, b)
    x0, y0 = int(np.round(x0)), int(np.round(y0))
    return [[x0, y0]]


def segmented_intersections(lines):
    #Finds the intersections between groups of lines.

    intersections = []
    for i, group in enumerate(lines[:-1]):
        for next_group in lines[i+1:]:
            for line1 in group:
                for line2 in next_group:
                    intersections.append(intersection(line1, line2)) 
    return intersections

def rect_from_crossings(crossings):
    #find all rectangles without other points inside
    rectangles = []

    # Search all possible rectangles
    for i in range(len(crossings)):
        x1= int(crossings[i][0][0])
        y1= int(crossings[i][0][1])

        for j in range(len(crossings)):
            x2= int(crossings[j][0][0])
            y2= int(crossings[j][0][1])

            #Search all points
            flag = 1
            for k in range(len(crossings)):
                x3= int(crossings[k][0][0])
                y3= int(crossings[k][0][1])

                #Dont count double (reverse rectangles)
                if (x1 > x2 or y1 > y2):
                    flag = 0
                #Dont count rectangles with points inside   
                elif ((((x3 >= x1) and (x2 >= x3))and (y3 > y1) and (y2 > y3) or ((x3 > x1) and (x2 > x3))and (y3 >= y1) and (y2 >= y3))):    
                    if(i!=k and j!=k):    
                        flag = 0

            if flag:
                rectangles.append([[x1,y1],[x2,y2]])

    return rectangles

if __name__ == '__main__':
    #img = cv2.imread('TAJFp.jpg')
    #img = cv2.imread('Bj2uu.jpg')
    img = cv2.imread('yi8db.png')

    width = int(img.shape[1])
    height = int(img.shape[0])

    scale = 380/width
    dim = (int(width*scale), int(height*scale))
    # resize image
    img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA) 

    img2 = img.copy()
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray,(5,5),cv2.BORDER_DEFAULT)

    # Parameters of Canny and Hough may have to be tweaked to work for as many cards as possible
    edges = cv2.Canny(gray,10,45,apertureSize = 7)
    lines = cv2.HoughLines(edges,1,np.pi/90,160)

    segmented = segment_by_angle_kmeans(lines)
    crossings = segmented_intersections(segmented)
    rectangles = rect_from_crossings(crossings)

    #Find biggest remaining rectangle
    size = 0
    for i in range(len(rectangles)):
        x1 = rectangles[i][0][0]
        x2 = rectangles[i][1][0]
        y1 = rectangles[i][0][1]
        y2 = rectangles[i][1][1]

        if(size < (abs(x1-x2)*abs(y1-y2))):
            size = abs(x1-x2)*abs(y1-y2)
            x1_rect = x1
            x2_rect = x2
            y1_rect = y1
            y2_rect = y2

    cv2.rectangle(img2, (x1_rect,y1_rect), (x2_rect,y2_rect), (0,0,255), 2)
    roi = img[y1_rect:y2_rect, x1_rect:x2_rect]

    cv2.imshow("Output",roi)
    cv2.imwrite("Output.png", roi)
    cv2.waitKey()

These are the results with the samples you provided:

The code for finding line crossings can be found here: find intersection point of two lines drawn using houghlines opencv

You can read more about Hough Lines here.

這篇關于使用 OpenCV 從桌面游戲卡圖像中提取藝術品的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产一级一级毛片 | 成人在线一区二区 | 日本a视频 | 九九免费在线视频 | 欧美久久久久久久久 | 中文天堂在线一区 | 在线观看视频91 | 免费成人高清在线视频 | 亚洲精品久久久一区二区三区 | 午夜爱爱毛片xxxx视频免费看 | 久久久精 | 国产日韩欧美激情 | 日日摸天天添天天添破 | 日本不卡一区二区三区 | 高清黄色毛片 | 欧美福利专区 | 一区精品在线观看 | 请别相信他免费喜剧电影在线观看 | 91天堂网| 成人网视频 | 一区二区三区视频在线观看 | 一区中文 | 天天干天天爱天天操 | 久久精品无码一区二区三区 | 精品久久久久久亚洲综合网站 | 日韩精品在线一区 | 国产91在线观看 | 日韩久久久久久 | 成年人精品视频在线观看 | 国产一区二区不卡 | 中文字幕乱码一区二区三区 | 欧美一二三区 | 在线播放一区二区三区 | 99色在线 | 在线成人 | 九九热在线观看视频 | 欧美三区 | 99久久精品一区二区毛片吞精 | 成人综合一区二区 | 9191在线播放| 2023亚洲天堂 |