Python 如何获得感染区域的准确百分比?
我正在opencv python中创建一个预处理程序,用于计算总面积、感染区域和感兴趣区域的百分比Python 如何获得感染区域的准确百分比?,python,opencv,Python,Opencv,我正在opencv python中创建一个预处理程序,用于计算总面积、感染区域和感兴趣区域的百分比 import cv2 import numpy as np import argparse img1 = cv2.imread('19.jpg') img = cv2.resize(img1, (0,0), fx=0.5, fy=0.5) original = img.copy() neworiginal = img.copy() blur1 = cv2.Gaussian
import cv2
import numpy as np
import argparse
img1 = cv2.imread('19.jpg')
img = cv2.resize(img1, (0,0), fx=0.5, fy=0.5)
original = img.copy()
neworiginal = img.copy()
blur1 = cv2.GaussianBlur(img,(3,3),1)
newimg = np.zeros((img.shape[0], img.shape[1],3),np.uint8)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER , 10 ,1.0)
img = cv2.pyrMeanShiftFiltering(blur1, 20, 30, newimg, 0, criteria)
blur = cv2.GaussianBlur(img,(11,11),1)
kernel = np.ones((5,5),np.uint8)
canny = cv2.Canny(blur, 200, 290)
res = cv2.morphologyEx(canny,cv2.MORPH_CLOSE, kernel)
canny = cv2.cvtColor(canny,cv2.COLOR_GRAY2BGR)
cv2.imshow('Canny',res)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower = np.array([5,25,25])
upper = np.array([70,255,255])
mask = cv2.inRange(hsv, lower, upper)
res = cv2.bitwise_and(hsv,hsv, mask= mask)
gray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
for i in contours:
cnt = cv2.contourArea(i)
#M = cv2.momens(i)
#cx = int(M['m10']/M['m00'])
if cnt > 1000:
cv2.drawContours(img, [i], 0, (0,0,255), 2)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = max(contours, key=cv2.contourArea)
Tarea = cv2.contourArea(cnt)
cv2.imshow('img', img)
height, width, _ = canny.shape
min_x, min_y = width, height
max_x = max_y = 0
frame = canny.copy()
for contour, hier in zip(contours, hierarchy):
(x,y,w,h) = cv2.boundingRect(contour)
min_x, max_x = min(x, min_x), max(x+w, max_x)
min_y, max_y = min(y, min_y), max(y+h, max_y)
if w > 80 and h > 80:
cv2.rectangle(frame, (x,y), (x+w,y+h), (255, 0, 0), 2)
roi = img[y:y+h , x:x+w]
originalroi = original[y:y+h , x:x+w]
if max_x - min_x > 0 and max_y - min_y > 0:
cv2.rectangle(frame, (min_x, min_y), (max_x, max_y), (255, 0, 0), 2)
img = roi
imghls = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
imghls[np.where((imghls==[30,200,2]).all(axis=2))] = [0,200,0]
huehls = imghls[:,:,0]
huehls[np.where(huehls==[0])] = [35]
#Thresholding on hue image
ret, thresh = cv2.threshold(huehls,28,255,cv2.THRESH_BINARY_INV)
cv2.imshow('thresh', thresh)
mask = cv2.bitwise_and(originalroi,originalroi,mask = thresh)
_, contours,heirarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
Infarea = 0
for x in range(len(contours)):
cv2.drawContours(originalroi,contours[x],-1,(0,0,255),2)
cv2.imshow('Contour masked',originalroi)
#Calculating area of infected region
Infarea += cv2.contourArea(contours[x])
#if Infarea > Tarea:
#Tarea = img.shape[0]*img.shape[1]
print ('_______________________\n| Total area: ' + str(Tarea) + ' |\n|_____________________|')
#Finding the percentage of infection in the banana
print ('\n__________________________\n| Infected area: ' + str(Infarea) + ' |\n|________________________|')
try:
per = 100 * Infarea/Tarea
except ZeroDivisionError:
per = 0
print( '\n_________________________________________________\n| Percentage of infection region: ' + str(per) + ' |\n|_______________________________________________|')
cv2.imshow('orig',original)
这是我在这段特殊代码中使用的图像我的问题是获取感兴趣的区域它的结果不准确如果我们在自己的视力中看到感染区域,我想应该是50%。你知道如何获得感兴趣区域的最佳准确率,因为结果也不准确。如果你的背景与感染区域的颜色不同,你可以简单地使用色差。我使用C++,但是可以轻松地转换为Python。我的做法如下:
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "highgui.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
Mat src = imread( "/ur/image/directory/banana.jpg",CV_LOAD_IMAGE_GRAYSCALE );
imshow( "Source", src );
//check all pixels and change pixel values according to refrences
for(int i=0; i<src.rows; i++)
{
for(int j=0;j<src.cols;j++)
{
if(src.at<uchar>(Point(j,i))>200)
src.at<uchar>(Point(j,i)) = 0;
else if(src.at<uchar>(Point(j,i))<30)
src.at<uchar>(Point(j,i)) = 255;
else
src.at<uchar>(Point(j,i)) = 125;
}
}
imshow( "First Process", src );
Mat filtered_img;
medianBlur ( src, filtered_img, 5 );
imshow( "After Median", filtered_img );
double deformation_counter = 0;
double clean_counter = 0;
//check all pixels and count them
for(int i=0; i<src.rows; i++)
{
for(int j=0;j<src.cols;j++)
{
if(src.at<uchar>(Point(j,i))==255)
deformation_counter++;
if(src.at<uchar>(Point(j,i))==125)
clean_counter++;
}
}
double percentage = deformation_counter/(deformation_counter+clean_counter);
cout<<percentage<<endl;
waitKey(0);
return 0;
}
#包括“opencv2/imgproc/imgproc.hpp”
#包括“opencv2/highgui/highgui.hpp”
#包括“highgui.h”
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int main(int argc,字符**argv)
{
Mat src=imread(“/ur/image/directory/banana.jpg”,CV\u LOAD\u image\u GRAYSCALE);
imshow(“来源”,src);
//检查所有像素并根据参考更改像素值
对于(int i=0;iSurrounded contour(edge)和它上面的黑色百分比?是的,因为如果我们在视觉中看到此代码中的结果不完全正确。如果我们查看感染区域,它必须小于或等于50%,但感染区域的结果非常低,有没有办法获得感染区域的准确结果?为什么?刚刚删除:)感染区域是否会有更高的结果?我计算的百分比为0.28(%28)背景是通过制作黑色来消除的,所以我需要改变背景以获得更高的结果。不,我用代码改变了它。只是你的背景色不应该和香蕉和黑色一样。你能帮助在Python中转换吗?我会用它来作为我的参考,我不知道如何使用C++。