Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cclabel.py #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions cclabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
# by Kesheng Wu, Ekow Otoo, and Kenji Suzuki
#



import Image, ImageDraw


import sys
import math, random
from itertools import product
from ufarray import *
import numpy as np


def run(img):
def bwlabel(img):
data = img.load()
width, height = img.size

Expand Down Expand Up @@ -112,6 +117,7 @@ def run(img):
output_img = Image.new("RGB", (width, height))
outdata = output_img.load()

count=0
for (x, y) in labels:

# Name of the component the current point belongs to
Expand All @@ -123,12 +129,45 @@ def run(img):
# Associate a random color with this component
if component not in colors:
colors[component] = (random.randint(0,255), random.randint(0,255),random.randint(0,255))
count=count+1

# Colorize the image
outdata[x, y] = colors[component]

return (labels, output_img)
## EDIT
#Creating a 2D numpy array(of size width*height)
#Checks if there are more 1s than 0s and vice versa. It creates an array filled with zeros/ones appropriately
if((len(labels)*2) > width*height):
ccarr = np.ones((width, height))
else:
ccarr = np.zeros((width, height))

# EDIT :The labeling that is done above has a disjoint sequence of integers
#Eg: Components with labels 1, 2, 5 may exist. But there may be no components with labels 3 or 4.
#This is because in the second pass, components are combined and hence certain labels are lost

#Suppose there are 'N' components with labels c1, c2, ...,cN (where c1..cN is an arbitrary sequence of integers)
#Labelswap associates the above sequence {c1...cN} to {1....N}
#Also ccarr is now an array such that
# ccarr[i,j]=0 , if the pixel was originally zero.
# ccarr[i,j]=some label between 1 & N , if the pixel was originally part of a component

labelswap = {}
labelcount=1
for i in labels.keys():
lab = int(labels[i])
(a,b)=i
if lab in labelswap:
ccarr[a,b]=labelswap[lab]
else:
labelswap[lab]=labelcount
labelcount+=1
ccarr[a,b]=labelswap[lab]

# EDIT : ccarr is returned instead of labels
return (ccarr, count, output_img)


def main():
# Open the image
img = Image.open(sys.argv[1])
Expand All @@ -145,7 +184,7 @@ def main():
# will want to use
#
# output_image is just a frivolous way to visualize the components.
(labels, output_img) = run(img)
(ccarr, count, output_img) = bwlabel(img)

output_img.show()

Expand Down