python-opencv图像分割步骤_基于Python-OpenCV的图像分割目标位置检测

一种方法是利用肿瘤颜色较浅的特点进行颜色分割。我们首先提取大脑的ROI以防
大脑与一侧对齐,而不在图像的中心。从这里将图像转换为HSV颜色空间,定义一个较低和较高的颜色范围,然后使用^{}执行颜色阈值。这将给我们一个二进制掩码。从这里我们只需裁剪蒙版的左右两半,然后使用^{}计算每边的像素。像素数越高的那一面就是有肿瘤的那一面。在
Otsu阈值->检测到大脑ROI ->提取ROI
# 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]
对提取的感兴趣区域进行颜色分割后得到的二值掩模
python-opencv图像分割步骤_基于Python-OpenCV的图像分割目标位置检测
文章图片

^{pr2}$
裁剪左右两半
python-opencv图像分割步骤_基于Python-OpenCV的图像分割目标位置检测
文章图片
python-opencv图像分割步骤_基于Python-OpenCV的图像分割目标位置检测
文章图片
# 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)
【python-opencv图像分割步骤_基于Python-OpenCV的图像分割目标位置检测】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()

    推荐阅读