# 番茄熟度辨識處理
![](https://i.imgur.com/enF1ERM.png)
[番茄colab程式](https://colab.research.google.com/drive/1e_bWN7B3WPfxeuX5wlq4tinJtcgTMEZ5#scrollTo=V1pEfD9yEcBS)
yolo取番茄->圖去背->計算像素比->模糊規則->結果上傳
## YoloV5 摳圖
~~[yolov5結果取圖](https://blog.csdn.net/qq_36756866/article/details/116762837)
Caution
--resize大小
--路徑位置
--方框遮擋圖片 [解決辦法](https://blog.csdn.net/weixin_44834086/article/details/125250363)~~
<font color="red">2023/2/22更
YoloV5中 detect.py參數中的--save_crops就有裁切功能</font>✔️
## Pre-processing(預處理)
1.Resizing Images✔️
re-sized all images to 250 × 250
2.Background Removal
<font color="red">2023/02/25
處理辦法:
背景去除算法
API去背(花錢)
[重新label圖片訓練新模型](https://chtseng.wordpress.com/2020/06/18/labelme%E8%88%87coco%E5%BD%B1%E5%83%8F%E5%88%86%E5%89%B2%E6%A8%99%E8%A8%98/)</font>
2023/3/9 今日進度:label到134
[labelme使用](https://github.com/wkentaro/labelme#windows)
2023/5/2 放棄重新label,跳過此階段
3.Convert RGB to HSV color space
4.Extract(H,S,V) Compoents
## Feature extraction(特徵提取)
提取個顏色與原圖像素比
![](https://i.imgur.com/oZzYj0L.png)
![](https://i.imgur.com/VFrFGac.png)
![](https://i.imgur.com/wc08W4E.png)
## Generate fuzzy rule base for the system(模糊規則)
4 channel green,yellow,orange,red
4 degree xs,s,m,l
if rule 256 判別成熟度
4 result
(2023/5/2)程式:
```
import cv2
import numpy as np
import os
def ripeness_judgement(input_folder):
for filename in os.listdir(input_folder):
if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png"):
# 讀入圖片
img = cv2.imread(os.path.join(input_folder, filename))
# 轉換成 HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# 定義綠、黃、橘、紅的顏色範圍 (HSV)
lower_green = np.array([35, 43, 46])
upper_green = np.array([77, 255, 255])
lower_yellow = np.array([26, 43, 46])
upper_yellow = np.array([34, 255, 255])
lower_orange = np.array([11, 43, 46])
upper_orange = np.array([25, 255, 255])
lower_red = np.array([0, 43, 46])
upper_red = np.array([10, 255, 255])
# 進行色度分割
mask_green = cv2.inRange(hsv, lower_green, upper_green)
mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow)
mask_orange = cv2.inRange(hsv, lower_orange, upper_orange)
mask_red = cv2.inRange(hsv, lower_red, upper_red)
# 取得各個顏色部分的圖像
green_part = cv2.bitwise_and(img, img, mask=mask_green)
yellow_part = cv2.bitwise_and(img, img, mask=mask_yellow)
orange_part = cv2.bitwise_and(img, img, mask=mask_orange)
red_part = cv2.bitwise_and(img, img, mask=mask_red)
# 計算像素與去除綠色圖比例
total_pixels = np.count_nonzero(img)
green_pixels = cv2.countNonZero(mask_green)
yellow_pixels = cv2.countNonZero(mask_yellow)
orange_pixels = cv2.countNonZero(mask_orange)
red_pixels = cv2.countNonZero(mask_red)
green_ratio = green_pixels / total_pixels
yellow_ratio = yellow_pixels / total_pixels
orange_ratio = orange_pixels / total_pixels
red_ratio = red_pixels / total_pixels
print("{}\n".format(filename))
print("Green_ratio", green_ratio)
print("Yellow ratio:", yellow_ratio)
print("Orange ratio:", orange_ratio)
print("Red ratio:", red_ratio,"\n")
# 放大圖片
scale = 5
img = cv2.resize(img, (img.shape[1] * scale, img.shape[0] * scale))
green_part = cv2.resize(green_part, (green_part.shape[1] * scale, green_part.shape[0] * scale))
yellow_part = cv2.resize(yellow_part, (yellow_part.shape[1] * scale, yellow_part.shape[0] * scale))
orange_part = cv2.resize(orange_part, (orange_part.shape[1] * scale, orange_part.shape[0] * scale))
red_part = cv2.resize(red_part, (red_part.shape[1] * scale, red_part.shape[0] * scale))
if not os.path.exists("output"):
os.makedirs("output")
if not os.path.exists("output/{}".format(filename)):
if green_ratio >0.15 and orange_ratio <0.05 and red_ratio <0.05:
cv2.imwrite("output/Immature_{}.png".format(filename), img)
elif yellow_ratio > 0.05:
cv2.imwrite("output/Break_{}.png".format(filename), img)
elif orange_ratio > 0.1 and red_ratio < 0.08 and yellow_ratio < 0.6:
cv2.imwrite("output/Pre-harvest_{}.png".format(filename), img)
elif red_ratio > orange_ratio:
cv2.imwrite("output/Harvest_{}.png".format(filename), img)
else:
cv2.imwrite("output/unclear_{}.png".format(filename), img)
else:
if green_ratio >0.15 and orange_ratio <0.05 and red_ratio <0.05:
cv2.imwrite("output/Immature_{}_2.png".format(filename), img)
elif yellow_ratio > 0.05:
cv2.imwrite("output/Break_{}_2.png".format(filename), img)
elif orange_ratio > 0.1 and red_ratio < 0.08 and yellow_ratio < 0.6:
cv2.imwrite("output/Pre-harvest_{}_2.png".format(filename), img)
elif red_ratio > orange_ratio:
cv2.imwrite("output/Harvest_{}_2.png".format(filename), img)
else:
cv2.imwrite("output/unclear_{}_2.png".format(filename), img)
for k in range(1,3):
if k==1:
input_folder = r"crops\ripe"
print("ripe開始處理\n")
ripeness_judgement(input_folder)
elif k==2:
input_folder = r"crops\unripe"
print("unripe開始處理\n")
ripeness_judgement(input_folder)
```
## Result
![](https://i.imgur.com/8hfW84w.png)