无法使用python OpenCV识别图像中的完整网格

问题描述 投票:0回答:1

我目前正在执行图像处理任务,需要识别网格单元并使用每个补丁作为单元的网格来创建补丁。我可以使用图片下方的代码生成以下输出。enter image description here

import numpy as np
import pandas as pd
import time 
from datetime import datetime
# from PIL import Image 
import torch 
import os, sys
import math
import cv2


# In[11]:


image = cv2.imread('../data/classes2.jpg')
print('rgb img shape : ',image.shape)
mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
print('gray img shape : ',gray.shape)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]




# In[15]:


# Detect only grid
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# print(cnts)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    print(area)
    if area > 10000:
        cv2.drawContours(mask, [c], -1, (255,255,255), -1)


# In[16]:


mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
mask = cv2.bitwise_and(mask, thresh)


# In[17]:


# Find horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (55,1))
detect_horizontal = cv2.morphologyEx(mask, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(image, [c], -1, (0,0,255), 2)


# In[18]:


# Find vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,25))
detect_vertical = cv2.morphologyEx(mask, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(image, [c], -1, (0,0,255), 2)


# In[19]:


cv2.imshow('thresh', thresh)
cv2.imshow('mask', mask)
cv2.imshow('image', image)
cv2.waitKey()

但是,我无法在轮廓中完全识别出网格线。我什至尝试使图像更清晰,但仍然没有成功。有人可以帮我吗?我是图像处理的新手。原始图像为enter image description here

编辑:

一个补丁=一个网格单元即。网格的一个黄色单元格或橙色或白色单元格。我的目的是通过绘制轮廓来识别网格,然后逐个网格地裁剪网格单元,其中每个方形单元将充当补丁,然后我需要识别补丁/单元的颜色。这里每个补丁将被保存为图像。

我已经修改了代码,但是仍然缺少一些单元格。有没有更好的方法来标识细胞并将其裁剪为补丁并标识其颜色,而不是首先标识网格。目前,我得到以下输出:

enter image description here

对于上面的输出,我引用了link to grid identification in image python code中的代码

尝试使用硬编码,我得到了重叠的补丁。下面的代码是我尝试过的:

image = cv2.imread(image_path + 'classes2.jpg')
#claculate patch pixels
white_x_pixels = 5
white_y_pixels = 5
grid_rows, grid_cols = 50,51
patch_row_pixels = round((image.shape[0])/50,2)
patch_col_pixels = round((image.shape[1])/51,2)
print('patch_row_pixels : ',patch_row_pixels)
print('patch_col_pixels : ',patch_col_pixels)
w = ceil(patch_col_pixels) 
h = floor(patch_row_pixels) 
print('w:{},h:{}'.format(w,h))
image_number = 0
coord_dict = {}
for r in range(grid_rows):#(49,50):#(grid_rows): 
#     y = white_y_pixels + r*(h)
    if r in [0,1,2]:
        y = white_y_pixels + r*(h) 
    else : 
        y = r*(h) - 1
    yh = int(y+h) - white_y_pixels
    y = int(y)
    for c in range(grid_cols):#(49,51):#(grid_cols):#(1):#(grid_cols):
        coord_dict[image_number] = {}
        if c in [0,1,2]:
            x = white_x_pixels + c*(w)# - white_x_pixels)#patch_col_pixels
        else: 
            x = c*(w) - (white_x_pixels - 1)
        xw = int(x+w) - white_x_pixels
        x = int(x)
        ROI = image[y:yh,x:xw]
        coord_dict[image_number]['x'] = x
        coord_dict[image_number]['x+w'] = x+w
        coord_dict[image_number]['y'] = y
        coord_dict[image_number]['y+h'] = y+h
        coord_dict[image_number]['w'] = w
        coord_dict[image_number]['h'] = h
        print(x,xw,y,yh)
        cv2.imwrite('ROI_'+str(image_number) + '.png',ROI)
#         cv2.imshow('ROI_'+str(image_number),ROI)
#         cv2.waitKey(0)
#         cv2.destroyAllWindows()
        image_number += 1 
python opencv image-processing grid
1个回答
0
投票

[抱歉,此后我没有跟进。我尝试对提供的图像进行硬编码,但是图像稍微偏右,并且裁剪不正确(我懒得计算它们)。但是为了展示如何做到,我裁剪了图像的一小部分。

enter image description here

您可以使用原始图像尝试此代码。但是,您将需要正确裁剪并纠正该歪斜。

import cv2

img = cv2.imread('patches.jpg')
no_w = 7 # replace with no. of patches in width
no_h = 5 # replace with no. of patches in height
h, w = img.shape[:2]
pixels_w = round(w/no_w)
pixels_h = round(h/no_h)

for i in range(0, h, pixels_h):
    cv2.line(img, (0, i), (w, i), (0, 255, 0), 1)
for i in range(0, w, pixels_w):
    cv2.line(img, (i, 0), (i, h), (0, 255, 0), 1)

cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.