【发布时间】:2021-05-22 20:23:14
【问题描述】:
我是深度学习的新手,并尝试实现用于图像聚类的 ML 算法。问题是我无法使用 OpenCV 在 Python 中裁剪图像中的对象。 这是我实现的代码,如果对象的颜色与背景非常不同(以 RGB 值表示),它适用于某些对象,但它不适用于 ML 算法所需的图像。我应该拥有/更改什么样的参数?有什么建议吗?
import cv2
import numpy as np
from PIL import Image
import tkinter as tk
from tkinter import filedialog as fd
from tkinter import*
import random
#!/usr/bin/python
from PIL import Image
import sys
myFile = 'Path' + '/crop.png'
nr_of_im = 1
q = 0
r = 0
x_list = []
y_list = []
img = cv2.imread(myFile, cv2.IMREAD_UNCHANGED)
ret, thresh = cv2.threshold(cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY) , 30, 255, cv2.THRESH_BINARY)
contours, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
print("len",len(contours))
if cv2.contourArea(contour) > 80:
x, y, w, h = cv2.boundingRect(contour)
q = w
r = h
x_list.append(x)
y_list.append(y)
font = cv2.FONT_HERSHEY_SIMPLEX
ROI = img[y-10:y+10+h, x-10:x+10+w]
ROI = cv2.resize(ROI,(300,300))
file_all = "/images/%d.jpg"%nr_of_im
nr_of_im += 1
cv2.imwrite(file_all,ROI)
图像中有21个物体,但轮廓的长度返回1。图像看起来像这样
crop.png:
【问题讨论】:
标签: python opencv opencv-contour