水
稻
分
割
问
题
水稻分割问题
水稻分割问题
一 深度模型分割
img_raw = cv2.imread('r1.png')
# img_raw.shape
# begin = 0
# gap = 400
block1_1 = img_raw[0:400,0:400]
block1_2 = img_raw[0:400,400:800]
block1_3 = img_raw[0:400,800:1200]
block1_4 = img_raw[0:400,1200:1600]
block1_5 = img_raw[0:400,1600:2000]
# cv2.imshow('block1_1', block1_1)
# cv2.imshow('block1_2', block1_2)
# cv2.imshow('block1_3', block1_3)
# cv2.imshow('block1_4', block1_4)
# cv2.imshow('block1_5', block1_5)
block2_1 = img_raw[400:800,0:400]
block2_2 = img_raw[400:800,400:800]
block2_3 = img_raw[400:800,800:1200]
block2_4 = img_raw[400:800,1200:1600]
block2_5 = img_raw[400:800,1600:2000]
# cv2.imshow('block2_1', block2_1)
# cv2.imshow('block2_2', block2_2)
# cv2.imshow('block2_3', block2_3)
# cv2.imshow('block2_4', block2_4)
# cv2.imshow('block2_5', block2_5)
block3_1 = img_raw[800:1200,0:400]
block3_2 = img_raw[800:1200,400:800]
block3_3 = img_raw[800:1200,800:1200]
block3_4 = img_raw[800:1200,1200:1600]
block3_5 = img_raw[800:1200,1600:2000]
# cv2.imshow('block3_1', block3_1)
# cv2.imshow('block3_2', block3_2)
# cv2.imshow('block3_3', block3_3)
# cv2.imshow('block3_4', block3_4)
# cv2.imshow('block3_5', block3_5)
cv2.imwrite(r"./dataset/image/1_block1_1.png", block1_1)
cv2.imwrite(r"./dataset/image/1_block1_2.png", block1_2)
cv2.imwrite(r"./dataset/image/1_block1_3.png", block1_3)
cv2.imwrite(r"./dataset/image/1_block1_4.png", block1_4)
cv2.imwrite(r"./dataset/image/1_block1_5.png", block1_5)
cv2.imwrite(r"./dataset/image/1_block2_1.png", block2_1)
cv2.imwrite(r"./dataset/image/1_block2_2.png", block2_2)
cv2.imwrite(r"./dataset/image/1_block2_3.png", block2_3)
cv2.imwrite(r"./dataset/image/1_block2_4.png", block2_4)
cv2.imwrite(r"./dataset/image/1_block2_5.png", block2_5)
cv2.imwrite(r"./dataset/image/1_block3_1.png", block3_1)
cv2.imwrite(r"./dataset/image/1_block3_2.png", block3_2)
cv2.imwrite(r"./dataset/image/1_block3_3.png", block3_3)
cv2.imwrite(r"./dataset/image/1_block3_4.png", block3_4)
cv2.imwrite(r"./dataset/image/1_block3_5.png", block3_5)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
二 使用SIFT进行匹配分割
三 传统阈值分割
import cv2
import matplotlib.pyplot as plt
import numpy as np
def traditional_seg(img_path,img_i,img_j):
img_path = img_path
img = cv2.imread(img_path, 0)
threshold = 127
ret, mask = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)
h,w = mask.shape
img_raw = cv2.imread(img_path)
x_y_bg =[]
for row in range(h):
for col in range(w):
if mask[row][col] == 0:
x_y_bg.append((row,col))
for i,j in x_y_bg:
img_raw[i,j] = [0,0,0]
for i in range(400):
for j in range(400):
if img_raw[i,j,1]<200 and img_raw[i,j,2]<200:
img_raw[i,j] =[0,0,0]
cv2.imshow('img_raw_second_seg', img_raw)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('second.png', img_raw)
img_sec_path = 'second.png'
# 灰度图读入
img = cv2.imread(img_sec_path, 0)
threshold = 127
# 阈值分割
ret, mask = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)
# print(ret)
img_raw_dealing = cv2.imread(img_path)
contours, hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img_raw_dealing,contours,-1,(255,0,0),1)
cv2.imshow('img_raw', img_raw)
# cv2.imshow('mask', mask)
cv2.imshow("img", img_raw_dealing)
cv2.imwrite(str(img_i)+"_"+str(img_j)+'seg.png', img_raw_dealing)
cv2.waitKey(0)
cv2.destroyAllWindows()