问题描述
我一直在尝试计算下面的样本图像中的维生素A颗粒(淡蓝色的点)的数量。
我已经尝试过使用OpenCV在Python中做到这一点,在这里我尝试获取浅蓝色颗粒的百分比和深蓝色(糖)晶体的百分比,将它们相互关联,然后生成一个粗糙的根据浅蓝色粒子占据的面积进行估算。对我而言,这种方法的问题在于它与样品的实际维生素含量不一致(该特定样品的维生素A含量为1%)...
关于我将如何处理此问题的任何建议?
这是我最初的方法:
import cv2
import numpy as np
import os
import statistics
from collections import OrderedDict
directory = 'Batches/Experiments/Sweetoil/DilutedWithWater/'
results = OrderedDict()
# Function to analyse single/multiple image(s)
def analyse_image(nFilename):
# image file
filename = nFilename
# reads image as greyscale
img = cv2.imread(filename,0)
# binarizes image (black and white)
_,img = cv2.threshold(img,180,255,cv2.THRESH_BINARY)
# image processing operations
kernel = np.ones((2,2),dtype=int)
dilation = cv2.dilate(img,kernel,iterations=6)
closing = cv2.morphologyEx(dilation,cv2.MORPH_CLOSE,kernel)
erosion = cv2.erode(closing,iterations=7)
# save image
cv2.imwrite(filename + '_Fluorescent.png',erosion)
cv2.imwrite('Fluorescent.png',erosion)
# clear memory
del dilation
del closing
del erosion
del kernel
del img
# reads image as greyscale
img = cv2.imread(filename,0)
# binarize image
_,115,cv2.THRESH_BINARY)
# save image
cv2.imwrite(filename + '_Sample.png',img)
cv2.imwrite('Sample.png',img)
del img
# Loads saved images
flr = cv2.imread('Fluorescent.png',0)
sample = cv2.imread('Sample.png',0)
# counts number of white pixels for both images
ctr_flr = np.count_nonzero(flr >= 250)
ctr_sample = np.count_nonzero(sample >= 250)
# Calculates percentage
percentage = (float(ctr_flr)/float(ctr_sample))*100
print(filename," - ",percentage,"%")
return percentage
def write_to_txt_file(content):
text_file = open(directory + "Analysis.txt","w")
text_file.write(content)
text_file.close()
pass
for entry in os.scandir(directory):
if (entry.path.endswith(".jpg")
or entry.path.endswith(".png")) and entry.is_file():
results[entry.path] = analyse_image(entry.path)
# displays the number of pixels and percentage
batch_avg = statistics.mean(results.values())
analysis = "Analysis:\n"
for key,value in results.items():
analysis += str(key) + "\n"
for key,value in results.items():
analysis += str(value) + "\n"
print("\nBatch average: " + str(batch_avg) + "%")
result_for_txt = "Directory: \n" + directory + "\n\n" + str(analysis) + "\n" + "Batch average:\n" + str(batch_avg) + "%"
write_to_txt_file(result_for_txt)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)