np.sum Not returning total counts

2024/10/6 9:27:42

I am trying this code but it does not return total count for zero[x][y], in this case it should return 5 but all it displays 255 five time.
THIS CODE IS FOR CONNECTED COMPONENTS AND ZERO IS ONE COMPONENT FOR WHITE PIXEL

Expected Output should be:5
But what i am getting is:
255
255
255
255
255

for (x, y) in labels:component = uf.find(labels[(x, y)])# Update the labels with correct informationlabels[(x, y)] = componentpath='imagesNew/13/'if labels[(x, y)]==0:Zero[y][x]=int(255)Z=Zero[y][x]print Zif Z==5:print Zero[y][x]Zeroth = Image.fromarray(Zero)Zeroth.save(path+'Zero'+'.png','png')
Answer

I believe this is what you're after. This code will loop through each label in the labeled image, create a boolean mask showing where each label is in the image, and sum that mask to tell you the total count of each label.

import numpy as np
labeled_img = np.array([[1, 1, 0, 0, 2],[1, 0, 0, 2, 2],[1, 0, 2, 2, 2],[1, 1, 1, 2, 2],[1, 1, 1, 2, 2]])for label in np.unique(labeled_img):print('Quantity of', label, 'labels:', np.sum(labeled_img == label))

Quantity of 0 labels: 5
Quantity of 1 labels: 10
Quantity of 2 labels: 10

Note that if you used OpenCV to find the connected components, the first return value of connectedComponents() is the number of labels, so you can simply loop over for label in range(n_labels) instead of np.unique(), which would be a tad faster.

https://en.xdnf.cn/q/120592.html

Related Q&A

Python Printing Dictionary Key and Value side by side

I want a program that prints Key and Value side by side for the following code:This is a Dictionary:d = {M: [Name1, Name2, Name3], F: [Name1,Name2,Name3]}I want the a program that prints in the followi…

can i use rusts match in python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about programming within the scope defined in the help center.Cl…

Consolidating Elements of a List [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

html textarea via python using post function

<div><textarea cols="25" rows="12" name="box" id="box" tabindex="2">TEXT GOES HERE </textarea></div><div><p class=&quo…

Fibonacci in Python - Simple solution [duplicate]

This question already has answers here:How to write the Fibonacci Sequence?(67 answers)Closed 6 years ago.n1 = 1 n2 = 1 n3 = n1 + n2 for i in range(10):n1 + n2print(n3)n1 = n2n2 = n3According to what …

snake game: snake colliding with itself

Hi I am currently writing a snake game code and I am nearly finished however I am having difficulty writing a code which will cause the game to end if the head of the snake collides with its body, I th…

python3 function not defined even though it is

when I try to call the changeProfile function, I keep getting the error "getAuthCode is not defined" even though it is clearly defined. Why do I keep getting this error? What am I doing wron…

Python maximum and minimum

Im supposed to write a function max_and_min that accepts a tuple containing integer elements as an argument and returns the largest and smallest integer within the tuple. The return value should be a t…

Get differences between two excel files

Problem SummaryGiven 2 excel files, each with 200 columns approx, and have a common index column - ie each row in both files would have a name property say, what would be the best to generate an output…

Most pythonic way to call a list of functions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…