Open CV Error: Multiple bounding boxes are being created around one single object during automated annotation...
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to automate bounding box creation process for my train and test images using Open CV.
The error I am receiving is : multiple bounding boxes are being created around one single object.
Where the original image(before annotation) is like this:
I am giving below the code I used:
import cv2
import numpy as np
from os import listdir
from os.path import isfile, join
import numpy
import config
mypath = config.mypath
xx = mypath.split('/')
folder_path = xx[-2]
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = numpy.empty(len(onlyfiles), dtype=object)
for n in range(0, len(onlyfiles)):
#images[n] = cv2.imread( join(mypath,onlyfiles[n]) )
images[n] = cv2.imread( join(mypath,onlyfiles[n]),1 )
xy = onlyfiles[n].split('.')
print(xy[0])
#img = cv2.imread('345.jpg')
img = images[n]
#print(images[n])
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# binarize the image
ret, bw = cv2.threshold(gray, 128, 255,
cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# find connected components
connectivity = 4
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(bw, connectivity, cv2.CV_32S)
sizes = stats[1:, -1]; nb_components = nb_components - 1
min_size = 25000 #threshhold value for objects in scene
img2 = np.zeros((img.shape), np.uint8)
for i in range(0, nb_components+1):
#print(i)
# use if sizes[i] >= min_size: to identify your objects
color = np.random.randint(255,size=3)
x = (stats[i][2])
if (650>x>130):
#cv2.rectangle(img, (217,203),(349,335), (0,255,0), 2)
x1 = stats[i][0]
x2 = stats[i][1]
x3 = stats[i][0]+stats[i][2]
x4 = stats[i][1]+stats[i][3]
cv2.rectangle(img, (x1,x2),(x3,x4), (0,255,0), 2)
print((x1,x2),(x3,x4))
#print (stats[i][2])
# draw the bounding rectangele around each object
img2[output == i + 1] = color
folder_xml = folder_path
filename_xml = (onlyfiles[n])
path_xml1 = (join(mypath,onlyfiles[n]))
path_xml = path_xml1.replace("/","\")
name_xml = folder_path
xmin_xml = stats[i][0]
ymin_xml = stats[i][1]
xmax_xml = stats[i][0]+stats[i][2]
ymax_xml = stats[i][1]+stats[i][3]
xml_name = xy[0]
#print((onlyfiles[n]))
#plt.imshow(img)
cv2.imshow('frame',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#!/usr/bin/python
#-*- coding: utf-8 -*-
#text.encode('UTF-8')
Please help me to understand what could possibly be wrong?
Thanks in advance!
python opencv annotations cv2
add a comment |
I am trying to automate bounding box creation process for my train and test images using Open CV.
The error I am receiving is : multiple bounding boxes are being created around one single object.
Where the original image(before annotation) is like this:
I am giving below the code I used:
import cv2
import numpy as np
from os import listdir
from os.path import isfile, join
import numpy
import config
mypath = config.mypath
xx = mypath.split('/')
folder_path = xx[-2]
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = numpy.empty(len(onlyfiles), dtype=object)
for n in range(0, len(onlyfiles)):
#images[n] = cv2.imread( join(mypath,onlyfiles[n]) )
images[n] = cv2.imread( join(mypath,onlyfiles[n]),1 )
xy = onlyfiles[n].split('.')
print(xy[0])
#img = cv2.imread('345.jpg')
img = images[n]
#print(images[n])
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# binarize the image
ret, bw = cv2.threshold(gray, 128, 255,
cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# find connected components
connectivity = 4
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(bw, connectivity, cv2.CV_32S)
sizes = stats[1:, -1]; nb_components = nb_components - 1
min_size = 25000 #threshhold value for objects in scene
img2 = np.zeros((img.shape), np.uint8)
for i in range(0, nb_components+1):
#print(i)
# use if sizes[i] >= min_size: to identify your objects
color = np.random.randint(255,size=3)
x = (stats[i][2])
if (650>x>130):
#cv2.rectangle(img, (217,203),(349,335), (0,255,0), 2)
x1 = stats[i][0]
x2 = stats[i][1]
x3 = stats[i][0]+stats[i][2]
x4 = stats[i][1]+stats[i][3]
cv2.rectangle(img, (x1,x2),(x3,x4), (0,255,0), 2)
print((x1,x2),(x3,x4))
#print (stats[i][2])
# draw the bounding rectangele around each object
img2[output == i + 1] = color
folder_xml = folder_path
filename_xml = (onlyfiles[n])
path_xml1 = (join(mypath,onlyfiles[n]))
path_xml = path_xml1.replace("/","\")
name_xml = folder_path
xmin_xml = stats[i][0]
ymin_xml = stats[i][1]
xmax_xml = stats[i][0]+stats[i][2]
ymax_xml = stats[i][1]+stats[i][3]
xml_name = xy[0]
#print((onlyfiles[n]))
#plt.imshow(img)
cv2.imshow('frame',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#!/usr/bin/python
#-*- coding: utf-8 -*-
#text.encode('UTF-8')
Please help me to understand what could possibly be wrong?
Thanks in advance!
python opencv annotations cv2
please post the original image
– Ishara Madhawa
Jan 3 at 9:43
@IsharaMadhawa:I have edited my original post and now you will get to see the original image as well.
– Summa
Jan 3 at 10:12
add a comment |
I am trying to automate bounding box creation process for my train and test images using Open CV.
The error I am receiving is : multiple bounding boxes are being created around one single object.
Where the original image(before annotation) is like this:
I am giving below the code I used:
import cv2
import numpy as np
from os import listdir
from os.path import isfile, join
import numpy
import config
mypath = config.mypath
xx = mypath.split('/')
folder_path = xx[-2]
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = numpy.empty(len(onlyfiles), dtype=object)
for n in range(0, len(onlyfiles)):
#images[n] = cv2.imread( join(mypath,onlyfiles[n]) )
images[n] = cv2.imread( join(mypath,onlyfiles[n]),1 )
xy = onlyfiles[n].split('.')
print(xy[0])
#img = cv2.imread('345.jpg')
img = images[n]
#print(images[n])
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# binarize the image
ret, bw = cv2.threshold(gray, 128, 255,
cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# find connected components
connectivity = 4
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(bw, connectivity, cv2.CV_32S)
sizes = stats[1:, -1]; nb_components = nb_components - 1
min_size = 25000 #threshhold value for objects in scene
img2 = np.zeros((img.shape), np.uint8)
for i in range(0, nb_components+1):
#print(i)
# use if sizes[i] >= min_size: to identify your objects
color = np.random.randint(255,size=3)
x = (stats[i][2])
if (650>x>130):
#cv2.rectangle(img, (217,203),(349,335), (0,255,0), 2)
x1 = stats[i][0]
x2 = stats[i][1]
x3 = stats[i][0]+stats[i][2]
x4 = stats[i][1]+stats[i][3]
cv2.rectangle(img, (x1,x2),(x3,x4), (0,255,0), 2)
print((x1,x2),(x3,x4))
#print (stats[i][2])
# draw the bounding rectangele around each object
img2[output == i + 1] = color
folder_xml = folder_path
filename_xml = (onlyfiles[n])
path_xml1 = (join(mypath,onlyfiles[n]))
path_xml = path_xml1.replace("/","\")
name_xml = folder_path
xmin_xml = stats[i][0]
ymin_xml = stats[i][1]
xmax_xml = stats[i][0]+stats[i][2]
ymax_xml = stats[i][1]+stats[i][3]
xml_name = xy[0]
#print((onlyfiles[n]))
#plt.imshow(img)
cv2.imshow('frame',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#!/usr/bin/python
#-*- coding: utf-8 -*-
#text.encode('UTF-8')
Please help me to understand what could possibly be wrong?
Thanks in advance!
python opencv annotations cv2
I am trying to automate bounding box creation process for my train and test images using Open CV.
The error I am receiving is : multiple bounding boxes are being created around one single object.
Where the original image(before annotation) is like this:
I am giving below the code I used:
import cv2
import numpy as np
from os import listdir
from os.path import isfile, join
import numpy
import config
mypath = config.mypath
xx = mypath.split('/')
folder_path = xx[-2]
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = numpy.empty(len(onlyfiles), dtype=object)
for n in range(0, len(onlyfiles)):
#images[n] = cv2.imread( join(mypath,onlyfiles[n]) )
images[n] = cv2.imread( join(mypath,onlyfiles[n]),1 )
xy = onlyfiles[n].split('.')
print(xy[0])
#img = cv2.imread('345.jpg')
img = images[n]
#print(images[n])
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# binarize the image
ret, bw = cv2.threshold(gray, 128, 255,
cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# find connected components
connectivity = 4
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(bw, connectivity, cv2.CV_32S)
sizes = stats[1:, -1]; nb_components = nb_components - 1
min_size = 25000 #threshhold value for objects in scene
img2 = np.zeros((img.shape), np.uint8)
for i in range(0, nb_components+1):
#print(i)
# use if sizes[i] >= min_size: to identify your objects
color = np.random.randint(255,size=3)
x = (stats[i][2])
if (650>x>130):
#cv2.rectangle(img, (217,203),(349,335), (0,255,0), 2)
x1 = stats[i][0]
x2 = stats[i][1]
x3 = stats[i][0]+stats[i][2]
x4 = stats[i][1]+stats[i][3]
cv2.rectangle(img, (x1,x2),(x3,x4), (0,255,0), 2)
print((x1,x2),(x3,x4))
#print (stats[i][2])
# draw the bounding rectangele around each object
img2[output == i + 1] = color
folder_xml = folder_path
filename_xml = (onlyfiles[n])
path_xml1 = (join(mypath,onlyfiles[n]))
path_xml = path_xml1.replace("/","\")
name_xml = folder_path
xmin_xml = stats[i][0]
ymin_xml = stats[i][1]
xmax_xml = stats[i][0]+stats[i][2]
ymax_xml = stats[i][1]+stats[i][3]
xml_name = xy[0]
#print((onlyfiles[n]))
#plt.imshow(img)
cv2.imshow('frame',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#!/usr/bin/python
#-*- coding: utf-8 -*-
#text.encode('UTF-8')
Please help me to understand what could possibly be wrong?
Thanks in advance!
python opencv annotations cv2
python opencv annotations cv2
edited Jan 3 at 10:14
Summa
asked Jan 3 at 6:54
SummaSumma
227
227
please post the original image
– Ishara Madhawa
Jan 3 at 9:43
@IsharaMadhawa:I have edited my original post and now you will get to see the original image as well.
– Summa
Jan 3 at 10:12
add a comment |
please post the original image
– Ishara Madhawa
Jan 3 at 9:43
@IsharaMadhawa:I have edited my original post and now you will get to see the original image as well.
– Summa
Jan 3 at 10:12
please post the original image
– Ishara Madhawa
Jan 3 at 9:43
please post the original image
– Ishara Madhawa
Jan 3 at 9:43
@IsharaMadhawa:I have edited my original post and now you will get to see the original image as well.
– Summa
Jan 3 at 10:12
@IsharaMadhawa:I have edited my original post and now you will get to see the original image as well.
– Summa
Jan 3 at 10:12
add a comment |
1 Answer
1
active
oldest
votes
I got the solution myself; changing the range of 'x' (in the code shared above it is given as: (650>x>130)) resolved the issue
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017609%2fopen-cv-error-multiple-bounding-boxes-are-being-created-around-one-single-objec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I got the solution myself; changing the range of 'x' (in the code shared above it is given as: (650>x>130)) resolved the issue
add a comment |
I got the solution myself; changing the range of 'x' (in the code shared above it is given as: (650>x>130)) resolved the issue
add a comment |
I got the solution myself; changing the range of 'x' (in the code shared above it is given as: (650>x>130)) resolved the issue
I got the solution myself; changing the range of 'x' (in the code shared above it is given as: (650>x>130)) resolved the issue
answered Jan 4 at 5:13
SummaSumma
227
227
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017609%2fopen-cv-error-multiple-bounding-boxes-are-being-created-around-one-single-objec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
please post the original image
– Ishara Madhawa
Jan 3 at 9:43
@IsharaMadhawa:I have edited my original post and now you will get to see the original image as well.
– Summa
Jan 3 at 10:12