Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 3.x Python for loop是否只保存区域的最后一个值?_Python 3.x_Pandas_Matplotlib - Fatal编程技术网

Python 3.x Python for loop是否只保存区域的最后一个值?

Python 3.x Python for loop是否只保存区域的最后一个值?,python-3.x,pandas,matplotlib,Python 3.x,Pandas,Matplotlib,我使用代码的吹线进行轮廓检测和相应的面积计算,在打印面积时,所有值都会打印出来,但在保存CSV文件中只保存了最后一个值 import cv2 import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg import glob import os import pandas as pd img = cv2.imread('C:\pfm\segmented/L501.jpg') image

我使用代码的吹线进行轮廓检测和相应的面积计算,在打印面积时,所有值都会打印出来,但在保存CSV文件中只保存了最后一个值

import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import os
import pandas as pd


img = cv2.imread('C:\pfm\segmented/L501.jpg')
image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


from scipy import ndimage as nd
gaussian_img = nd.gaussian_filter(image, sigma=3)

ret, thresh = cv2.threshold(gaussian_img, 127,255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

#print ('No of shapes:', format(len(contours)))


for cnt in contours:

    M= cv2.moments(cnt)
        #print(M)

    if M["m00"] != 0:

        cx = int(M["m10"] / M["m00"])
        cy = int(M["m01"] / M["m00"])
    else:
        cx, cy = 0,0

    center = (cx,cy)


    cv2.drawContours(img, contours, -1, (0,255,0),2)


    plt.imshow(img)
    plt.imsave("C:\pfm\dataframe_csv\L501.jpg", img)

    area = cv2.contourArea(cnt)

    print(area)

    df = pd.DataFrame()
    df['Area'] = area
    df.to_csv("C:\pfm\dataframe_csv\L501.csv")

在代码中,在每个循环中重置
df
数据帧。
请尝试以下代码:

import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import os
import pandas as pd


img = cv2.imread('C:\pfm\segmented/L501.jpg')
image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


from scipy import ndimage as nd
gaussian_img = nd.gaussian_filter(image, sigma=3)

ret, thresh = cv2.threshold(gaussian_img, 127,255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

#print ('No of shapes:', format(len(contours)))

area_list = []
df = pd.DataFrame()

for cnt in contours:

    M= cv2.moments(cnt)
        #print(M)

    if M["m00"] != 0:

        cx = int(M["m10"] / M["m00"])
        cy = int(M["m01"] / M["m00"])
    else:
        cx, cy = 0,0

    center = (cx,cy)


    cv2.drawContours(img, contours, -1, (0,255,0),2)


    plt.imshow(img)
    plt.imsave("C:\pfm\dataframe_csv\L501.jpg", img)

    area = cv2.contourArea(cnt)

    print(area)

    area_list.append(area)

df['Area'] = area_list
df.to_csv("C:\pfm\dataframe_csv\L501.csv")

我没有将
区域
附加到每个循环中的
df
数据帧,而是将其附加到一个列表
area\u列表
。注意,我在
for
循环之前创建了这个空列表,以初始化它,以及
df
数据帧。当所有循环结束时,我通过将先前生成的列表保存在数据框中来创建数据框的
'Area'
列。通过这种方式,
df
不是空的,代码效率更高。

在代码中,在每个循环中,您都会重置
df
数据帧。
请尝试以下代码:

import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import os
import pandas as pd


img = cv2.imread('C:\pfm\segmented/L501.jpg')
image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


from scipy import ndimage as nd
gaussian_img = nd.gaussian_filter(image, sigma=3)

ret, thresh = cv2.threshold(gaussian_img, 127,255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

#print ('No of shapes:', format(len(contours)))

area_list = []
df = pd.DataFrame()

for cnt in contours:

    M= cv2.moments(cnt)
        #print(M)

    if M["m00"] != 0:

        cx = int(M["m10"] / M["m00"])
        cy = int(M["m01"] / M["m00"])
    else:
        cx, cy = 0,0

    center = (cx,cy)


    cv2.drawContours(img, contours, -1, (0,255,0),2)


    plt.imshow(img)
    plt.imsave("C:\pfm\dataframe_csv\L501.jpg", img)

    area = cv2.contourArea(cnt)

    print(area)

    area_list.append(area)

df['Area'] = area_list
df.to_csv("C:\pfm\dataframe_csv\L501.csv")
我没有将
区域
附加到每个循环中的
df
数据帧,而是将其附加到一个列表
area\u列表
。注意,我在
for
循环之前创建了这个空列表,以初始化它,以及
df
数据帧。当所有循环结束时,我通过将先前生成的列表保存在数据框中来创建数据框的
'Area'
列。通过这种方式,
df
不是空的,代码更有效。

已回答。已回答。