Extracting components from an edge image and storing for further processing
1Input
Given an edge image, I want to retrieve components in it one by one and store each component as an image so that I can use it later for processing. I guess this is called connected component labelling
For example, in the input image, there are 2 lines , 1 circle ,2 curves
I want to 5 image files containing these 5 components.
I was able to come up with code as below, but I do not know how to proceed further. Currently , I am getting all the components coloured in different colours in output
Thank you
import scipy
from skimage import io
from scipy import ndimage
import matplotlib.pyplot as plt
import cv2
import numpy as np
fname='..//Desktop//test1.png'
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#canny
img_canny = cv2.Canny(img,100,200)
threshold = 50
# find connected components
labeled, nr_objects = ndimage.label(img_canny)
print('Number of objects is %d'% nr_objects)
plt.imsave('..//Desktop//out.png', labeled)
Output
New output1
python opencv edge connected-components
add a comment |
1Input
Given an edge image, I want to retrieve components in it one by one and store each component as an image so that I can use it later for processing. I guess this is called connected component labelling
For example, in the input image, there are 2 lines , 1 circle ,2 curves
I want to 5 image files containing these 5 components.
I was able to come up with code as below, but I do not know how to proceed further. Currently , I am getting all the components coloured in different colours in output
Thank you
import scipy
from skimage import io
from scipy import ndimage
import matplotlib.pyplot as plt
import cv2
import numpy as np
fname='..//Desktop//test1.png'
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#canny
img_canny = cv2.Canny(img,100,200)
threshold = 50
# find connected components
labeled, nr_objects = ndimage.label(img_canny)
print('Number of objects is %d'% nr_objects)
plt.imsave('..//Desktop//out.png', labeled)
Output
New output1
python opencv edge connected-components
add a comment |
1Input
Given an edge image, I want to retrieve components in it one by one and store each component as an image so that I can use it later for processing. I guess this is called connected component labelling
For example, in the input image, there are 2 lines , 1 circle ,2 curves
I want to 5 image files containing these 5 components.
I was able to come up with code as below, but I do not know how to proceed further. Currently , I am getting all the components coloured in different colours in output
Thank you
import scipy
from skimage import io
from scipy import ndimage
import matplotlib.pyplot as plt
import cv2
import numpy as np
fname='..//Desktop//test1.png'
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#canny
img_canny = cv2.Canny(img,100,200)
threshold = 50
# find connected components
labeled, nr_objects = ndimage.label(img_canny)
print('Number of objects is %d'% nr_objects)
plt.imsave('..//Desktop//out.png', labeled)
Output
New output1
python opencv edge connected-components
1Input
Given an edge image, I want to retrieve components in it one by one and store each component as an image so that I can use it later for processing. I guess this is called connected component labelling
For example, in the input image, there are 2 lines , 1 circle ,2 curves
I want to 5 image files containing these 5 components.
I was able to come up with code as below, but I do not know how to proceed further. Currently , I am getting all the components coloured in different colours in output
Thank you
import scipy
from skimage import io
from scipy import ndimage
import matplotlib.pyplot as plt
import cv2
import numpy as np
fname='..//Desktop//test1.png'
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#canny
img_canny = cv2.Canny(img,100,200)
threshold = 50
# find connected components
labeled, nr_objects = ndimage.label(img_canny)
print('Number of objects is %d'% nr_objects)
plt.imsave('..//Desktop//out.png', labeled)
Output
New output1
python opencv edge connected-components
python opencv edge connected-components
edited Jan 1 at 12:07
Azhar
asked Jan 1 at 10:43
AzharAzhar
95
95
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You may not need to use cv2.canny()
to segment the contours, you can simply use binary thresholding technique as:
img = cv2.imread("/path/to/img.png")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 130, 255, cv2.THRESH_BINARY_INV)
# Opencv v3.x
im, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for i in xrange(len(contours)):
rect = cv2.boundingRect(contours[i])
contour_component = img[rect[1]:rect[1] + rect[3], rect[0]:rect[0] + rect[2]]
cv2.imwrite("component_{}.png".format(i), contour_component)
1
thank you so much @ZdaR . It is working ..Thanks a lot
– Azhar
Jan 1 at 12:33
Glad to help you :)
– ZdaR
Jan 1 at 12:34
add a comment |
This:
num=nr_objects
i=0
while i<num:
plt.imshow(labeled)
i=i+1
Does not loop over different labels, it just shows the same image num
times. You need to do something like:
for i in range(num):
tmp = np.zeros(labeled.shape)
tmp[labeled == i] = 255
plt.imshow(tmp)
Then you will see one for each label. Also you can use a for loop... If you have any questions leave a comment.
Thank you @api55 Output: Number of objects is 149( i guess i have to store 149 components) Getting only one image like this i.stack.imgur.com/FD7wZ.jpg
– Azhar
Jan 1 at 12:03
@Azhar That's because you are using canny and not all the components are touching each other. You can try using connected components and then canny or findContours, that is what ZdaR suggests :)
– api55
Jan 1 at 13:26
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%2f53994812%2fextracting-components-from-an-edge-image-and-storing-for-further-processing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You may not need to use cv2.canny()
to segment the contours, you can simply use binary thresholding technique as:
img = cv2.imread("/path/to/img.png")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 130, 255, cv2.THRESH_BINARY_INV)
# Opencv v3.x
im, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for i in xrange(len(contours)):
rect = cv2.boundingRect(contours[i])
contour_component = img[rect[1]:rect[1] + rect[3], rect[0]:rect[0] + rect[2]]
cv2.imwrite("component_{}.png".format(i), contour_component)
1
thank you so much @ZdaR . It is working ..Thanks a lot
– Azhar
Jan 1 at 12:33
Glad to help you :)
– ZdaR
Jan 1 at 12:34
add a comment |
You may not need to use cv2.canny()
to segment the contours, you can simply use binary thresholding technique as:
img = cv2.imread("/path/to/img.png")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 130, 255, cv2.THRESH_BINARY_INV)
# Opencv v3.x
im, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for i in xrange(len(contours)):
rect = cv2.boundingRect(contours[i])
contour_component = img[rect[1]:rect[1] + rect[3], rect[0]:rect[0] + rect[2]]
cv2.imwrite("component_{}.png".format(i), contour_component)
1
thank you so much @ZdaR . It is working ..Thanks a lot
– Azhar
Jan 1 at 12:33
Glad to help you :)
– ZdaR
Jan 1 at 12:34
add a comment |
You may not need to use cv2.canny()
to segment the contours, you can simply use binary thresholding technique as:
img = cv2.imread("/path/to/img.png")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 130, 255, cv2.THRESH_BINARY_INV)
# Opencv v3.x
im, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for i in xrange(len(contours)):
rect = cv2.boundingRect(contours[i])
contour_component = img[rect[1]:rect[1] + rect[3], rect[0]:rect[0] + rect[2]]
cv2.imwrite("component_{}.png".format(i), contour_component)
You may not need to use cv2.canny()
to segment the contours, you can simply use binary thresholding technique as:
img = cv2.imread("/path/to/img.png")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 130, 255, cv2.THRESH_BINARY_INV)
# Opencv v3.x
im, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for i in xrange(len(contours)):
rect = cv2.boundingRect(contours[i])
contour_component = img[rect[1]:rect[1] + rect[3], rect[0]:rect[0] + rect[2]]
cv2.imwrite("component_{}.png".format(i), contour_component)
answered Jan 1 at 12:13
ZdaRZdaR
13.2k33553
13.2k33553
1
thank you so much @ZdaR . It is working ..Thanks a lot
– Azhar
Jan 1 at 12:33
Glad to help you :)
– ZdaR
Jan 1 at 12:34
add a comment |
1
thank you so much @ZdaR . It is working ..Thanks a lot
– Azhar
Jan 1 at 12:33
Glad to help you :)
– ZdaR
Jan 1 at 12:34
1
1
thank you so much @ZdaR . It is working ..Thanks a lot
– Azhar
Jan 1 at 12:33
thank you so much @ZdaR . It is working ..Thanks a lot
– Azhar
Jan 1 at 12:33
Glad to help you :)
– ZdaR
Jan 1 at 12:34
Glad to help you :)
– ZdaR
Jan 1 at 12:34
add a comment |
This:
num=nr_objects
i=0
while i<num:
plt.imshow(labeled)
i=i+1
Does not loop over different labels, it just shows the same image num
times. You need to do something like:
for i in range(num):
tmp = np.zeros(labeled.shape)
tmp[labeled == i] = 255
plt.imshow(tmp)
Then you will see one for each label. Also you can use a for loop... If you have any questions leave a comment.
Thank you @api55 Output: Number of objects is 149( i guess i have to store 149 components) Getting only one image like this i.stack.imgur.com/FD7wZ.jpg
– Azhar
Jan 1 at 12:03
@Azhar That's because you are using canny and not all the components are touching each other. You can try using connected components and then canny or findContours, that is what ZdaR suggests :)
– api55
Jan 1 at 13:26
add a comment |
This:
num=nr_objects
i=0
while i<num:
plt.imshow(labeled)
i=i+1
Does not loop over different labels, it just shows the same image num
times. You need to do something like:
for i in range(num):
tmp = np.zeros(labeled.shape)
tmp[labeled == i] = 255
plt.imshow(tmp)
Then you will see one for each label. Also you can use a for loop... If you have any questions leave a comment.
Thank you @api55 Output: Number of objects is 149( i guess i have to store 149 components) Getting only one image like this i.stack.imgur.com/FD7wZ.jpg
– Azhar
Jan 1 at 12:03
@Azhar That's because you are using canny and not all the components are touching each other. You can try using connected components and then canny or findContours, that is what ZdaR suggests :)
– api55
Jan 1 at 13:26
add a comment |
This:
num=nr_objects
i=0
while i<num:
plt.imshow(labeled)
i=i+1
Does not loop over different labels, it just shows the same image num
times. You need to do something like:
for i in range(num):
tmp = np.zeros(labeled.shape)
tmp[labeled == i] = 255
plt.imshow(tmp)
Then you will see one for each label. Also you can use a for loop... If you have any questions leave a comment.
This:
num=nr_objects
i=0
while i<num:
plt.imshow(labeled)
i=i+1
Does not loop over different labels, it just shows the same image num
times. You need to do something like:
for i in range(num):
tmp = np.zeros(labeled.shape)
tmp[labeled == i] = 255
plt.imshow(tmp)
Then you will see one for each label. Also you can use a for loop... If you have any questions leave a comment.
answered Jan 1 at 11:58
api55api55
6,99542545
6,99542545
Thank you @api55 Output: Number of objects is 149( i guess i have to store 149 components) Getting only one image like this i.stack.imgur.com/FD7wZ.jpg
– Azhar
Jan 1 at 12:03
@Azhar That's because you are using canny and not all the components are touching each other. You can try using connected components and then canny or findContours, that is what ZdaR suggests :)
– api55
Jan 1 at 13:26
add a comment |
Thank you @api55 Output: Number of objects is 149( i guess i have to store 149 components) Getting only one image like this i.stack.imgur.com/FD7wZ.jpg
– Azhar
Jan 1 at 12:03
@Azhar That's because you are using canny and not all the components are touching each other. You can try using connected components and then canny or findContours, that is what ZdaR suggests :)
– api55
Jan 1 at 13:26
Thank you @api55 Output: Number of objects is 149( i guess i have to store 149 components) Getting only one image like this i.stack.imgur.com/FD7wZ.jpg
– Azhar
Jan 1 at 12:03
Thank you @api55 Output: Number of objects is 149( i guess i have to store 149 components) Getting only one image like this i.stack.imgur.com/FD7wZ.jpg
– Azhar
Jan 1 at 12:03
@Azhar That's because you are using canny and not all the components are touching each other. You can try using connected components and then canny or findContours, that is what ZdaR suggests :)
– api55
Jan 1 at 13:26
@Azhar That's because you are using canny and not all the components are touching each other. You can try using connected components and then canny or findContours, that is what ZdaR suggests :)
– api55
Jan 1 at 13:26
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%2f53994812%2fextracting-components-from-an-edge-image-and-storing-for-further-processing%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