Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 共定位对象计数_Python_Opencv_Image Processing_Counting - Fatal编程技术网

Python 共定位对象计数

Python 共定位对象计数,python,opencv,image-processing,counting,Python,Opencv,Image Processing,Counting,我正在尝试编写一个代码来自动计算图形中的圆形对象。对于我来说,我想计算包含蓝色、红色和蓝色+红色染色的圆形(图像中的细胞核)的数量。到目前为止,我已经得到了一个计算所有蓝色和红色圆圈的代码。我很难编写/修改代码以获得蓝色和红色的圆圈(共定位)。我已经在Google Colab上写了这篇文章。下面是输出一个.csv文件的代码,该文件的文件名计数为用户上传图像中的红色、绿色和蓝色圆圈 from google.colab import files from PIL import Image impor

我正在尝试编写一个代码来自动计算图形中的圆形对象。对于我来说,我想计算包含蓝色、红色和蓝色+红色染色的圆形(图像中的细胞核)的数量。到目前为止,我已经得到了一个计算所有蓝色和红色圆圈的代码。我很难编写/修改代码以获得蓝色和红色的圆圈(共定位)。我已经在Google Colab上写了这篇文章。下面是输出一个.csv文件的代码,该文件的文件名计数为用户上传图像中的红色、绿色和蓝色圆圈

from google.colab import files
from PIL import Image
import matplotlib.pyplot as plt
import cv2
import numpy as np
import pandas as pd

object_count = dict()
image_list = []

#### Object Counter Class
class ObjectCounter:
    
    def count(self):
        genotype = input('Genotype: ') 
        nImages = input('Number of images to quantify: ')
        a = 0

        while a < int(nImages):
          uploaded = files.upload()
          fileName = input('File name: ')
          image_list.append(fileName)

          low_intensity = int(input('Lower intensity: '))

          img = cv2.imread(fileName)
          img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
          plt.imshow(img)
          plt.show()

          r,g,b = cv2.split(img)

          colors = ['Red', 'Green', 'Blue']
          channels = [r, g, b]
          x = 0

          for i in channels:
            seed_pt = (20, 20)
            fill_color = 0
            mask = np.zeros_like(i)
            kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

            for th in range(low_intensity, 255):
              prev_mask = mask.copy()
              mask = cv2.threshold(i, th, 255, cv2.THRESH_BINARY)[1]
              mask = cv2.floodFill(mask, None, seed_pt, fill_color)[1]
              mask = cv2.bitwise_or(mask, prev_mask)
              mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)

            plt.imshow(mask)
            plt.show()

            n_centers = cv2.connectedComponents(mask)[0] - 1
            print('There are ' + str(n_centers) + ' objects in the image.')

            if colors[x] in object_count:
              object_count[colors[x]].append(n_centers)
            
            else:
              object_count[colors[x]] = [n_centers]
            
            x = x + 1
          
          df1 = pd.DataFrame(image_list,columns=['File Name'])         
          df2 = pd.DataFrame.from_dict(object_count)

          a = a + 1
        
        # Save the final counting data
        data = pd.concat([df1, df2], axis=1)
        print(data)

        output_fileName = input('Output .csv File Name: ')
        data.to_csv(output_fileName)
        files.download(output_fileName)

## Main Function 
countObj = ObjectCounter()
countObj.count()
从google.colab导入文件
从PIL导入图像
将matplotlib.pyplot作为plt导入
进口cv2
将numpy作为np导入
作为pd进口熊猫
对象计数=dict()
图像_列表=[]
####对象计数器类
类对象计数器:
def计数(自身):
基因型=输入('基因型:')
nImages=input('要量化的图像数:')
a=0
而a
下面是一个可以用来测试代码的示例图像。点击

有人能帮我更新/改进此代码,以获得蓝色和红色圆圈的数量吗

提前感谢,

卡桑