gpt4 book ai didi

python - 使用 Python OpenCV 检测图像中的对象位置

转载 作者:行者123 更新时间:2023-12-01 15:12:12 24 4
gpt4 key购买 nike

我需要在图像中找到下方肿瘤的位置,作为大脑的左侧或右侧。

current image


我尝试使用轮廓和 Canny 边缘检测来检测侧面,但似乎不起作用

# Find Canny edges 
edged = cv2.Canny(img, 30, 200)
cv2.waitKey(0)

# Finding Contours
# Use a copy of the image e.g. edged.copy()
# since findContours alters the image
contours, hierarchy = cv2.findContours(edged,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

cv2.imshow('Canny Edges After Contouring', edged)
cv2.waitKey(0)

print("Number of Contours found = " + str(len(contours)))

# Draw all contours
# -1 signifies drawing all contours
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)

最佳答案

一种方法是利用肿瘤颜色较浅的观察结果来执行颜色分割。我们首先提取大脑 ROI,以防万一
大脑与一侧对齐,但不在图像的中心。从这里将图像转换为 HSV 颜色空间,定义下限和上限颜色范围,然后使用 cv2.inRange() 执行颜色阈值处理.这将为我们提供一个二进制掩码。从这里我们简单地裁剪 mask 的左右两半,然后使用 cv2.countNonZero() 计算每一侧的像素.具有较高像素数的一侧将是具有肿瘤的一侧。

大津的阈值->检测到的大脑 ROI ->提取的投资返回率
enter image description here
enter image description here
enter image description here

# Load image, grayscale, Otsu's threshold, and extract ROI
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
x,y,w,h = cv2.boundingRect(thresh)
ROI = image[y:y+h, x:x+w]
对提取的 ROI 进行颜色分割后得到的二值掩码
enter image description here
# Color segmentation on ROI
hsv = cv2.cvtColor(ROI, cv2.COLOR_BGR2HSV)
lower = np.array([0, 0, 152])
upper = np.array([179, 255, 255])
mask = cv2.inRange(hsv, lower, upper)
裁剪的左右两半
enter image description here
enter image description here
# Crop left and right half of mask
x, y, w, h = 0, 0, image.shape[1]//2, image.shape[0]
left = mask[y:y+h, x:x+w]
right = mask[y:y+h, x+w:x+w+w]
每半像素数

Left pixels: 1252

Right pixels: 12

# Count pixels
left_pixels = cv2.countNonZero(left)
right_pixels = cv2.countNonZero(right)
由于左半部分像素较多,因此肿瘤位于 大脑的一半

完整代码
import numpy as np
import cv2

# Load image, grayscale, Otsu's threshold, and extract ROI
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
x,y,w,h = cv2.boundingRect(thresh)
ROI = image[y:y+h, x:x+w]

# Color segmentation on ROI
hsv = cv2.cvtColor(ROI, cv2.COLOR_BGR2HSV)
lower = np.array([0, 0, 152])
upper = np.array([179, 255, 255])
mask = cv2.inRange(hsv, lower, upper)

# Crop left and right half of mask
x, y, w, h = 0, 0, ROI.shape[1]//2, ROI.shape[0]
left = mask[y:y+h, x:x+w]
right = mask[y:y+h, x+w:x+w+w]

# Count pixels
left_pixels = cv2.countNonZero(left)
right_pixels = cv2.countNonZero(right)

print('Left pixels:', left_pixels)
print('Right pixels:', right_pixels)

cv2.imshow('mask', mask)
cv2.imshow('thresh', thresh)
cv2.imshow('ROI', ROI)
cv2.imshow('left', left)
cv2.imshow('right', right)
cv2.waitKey()
我使用这个 HSV 颜色阈值脚本来确定下限和上限颜色范围
import cv2
import sys
import numpy as np

def nothing(x):
pass

# Create a window
cv2.namedWindow('image')

# create trackbars for color change
cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv
cv2.createTrackbar('SMin','image',0,255,nothing)
cv2.createTrackbar('VMin','image',0,255,nothing)
cv2.createTrackbar('HMax','image',0,179,nothing)
cv2.createTrackbar('SMax','image',0,255,nothing)
cv2.createTrackbar('VMax','image',0,255,nothing)

# Set default value for MAX HSV trackbars.
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)

# Initialize to check if HSV min/max value changes
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0

img = cv2.imread('1.jpg')
output = img
waitTime = 33

while(1):

# get current positions of all trackbars
hMin = cv2.getTrackbarPos('HMin','image')
sMin = cv2.getTrackbarPos('SMin','image')
vMin = cv2.getTrackbarPos('VMin','image')

hMax = cv2.getTrackbarPos('HMax','image')
sMax = cv2.getTrackbarPos('SMax','image')
vMax = cv2.getTrackbarPos('VMax','image')

# Set minimum and max HSV values to display
lower = np.array([hMin, sMin, vMin])
upper = np.array([hMax, sMax, vMax])

# Create HSV Image and threshold into a range.
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
output = cv2.bitwise_and(img,img, mask= mask)

# Print if there is a change in HSV value
if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
phMin = hMin
psMin = sMin
pvMin = vMin
phMax = hMax
psMax = sMax
pvMax = vMax

# Display output image
cv2.imshow('image',output)

# Wait longer to prevent freeze for videos.
if cv2.waitKey(waitTime) & 0xFF == ord('q'):
break

cv2.destroyAllWindows()

关于python - 使用 Python OpenCV 检测图像中的对象位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59525065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com