Finding first non-zero value in an image
I'm pretty new to Python and I would like to locate the extremes of a binary image. There's a white shape in the middle of a black background and I would like to locate the top, bottom, left and right enclosing rectangle.
My way of doing this is by finding the first non-zero pixels in all directions.
My function is goes as this, but it only works on the Y axis. How can I manage to pass through the X axis?
def first_non_zero(img):
width = img.shape[1]
height = img.shape[0]
idx = 0
result = 0
for j in range(0, height):
idx = np.argmax(img[j])
if idx > 0:
result = j
break
return result
python numpy opencv
add a comment |
I'm pretty new to Python and I would like to locate the extremes of a binary image. There's a white shape in the middle of a black background and I would like to locate the top, bottom, left and right enclosing rectangle.
My way of doing this is by finding the first non-zero pixels in all directions.
My function is goes as this, but it only works on the Y axis. How can I manage to pass through the X axis?
def first_non_zero(img):
width = img.shape[1]
height = img.shape[0]
idx = 0
result = 0
for j in range(0, height):
idx = np.argmax(img[j])
if idx > 0:
result = j
break
return result
python numpy opencv
1
This may help... stackoverflow.com/a/51479636/2836621
– Mark Setchell
Nov 20 at 21:24
add a comment |
I'm pretty new to Python and I would like to locate the extremes of a binary image. There's a white shape in the middle of a black background and I would like to locate the top, bottom, left and right enclosing rectangle.
My way of doing this is by finding the first non-zero pixels in all directions.
My function is goes as this, but it only works on the Y axis. How can I manage to pass through the X axis?
def first_non_zero(img):
width = img.shape[1]
height = img.shape[0]
idx = 0
result = 0
for j in range(0, height):
idx = np.argmax(img[j])
if idx > 0:
result = j
break
return result
python numpy opencv
I'm pretty new to Python and I would like to locate the extremes of a binary image. There's a white shape in the middle of a black background and I would like to locate the top, bottom, left and right enclosing rectangle.
My way of doing this is by finding the first non-zero pixels in all directions.
My function is goes as this, but it only works on the Y axis. How can I manage to pass through the X axis?
def first_non_zero(img):
width = img.shape[1]
height = img.shape[0]
idx = 0
result = 0
for j in range(0, height):
idx = np.argmax(img[j])
if idx > 0:
result = j
break
return result
python numpy opencv
python numpy opencv
asked Nov 20 at 21:13
NickB
7510
7510
1
This may help... stackoverflow.com/a/51479636/2836621
– Mark Setchell
Nov 20 at 21:24
add a comment |
1
This may help... stackoverflow.com/a/51479636/2836621
– Mark Setchell
Nov 20 at 21:24
1
1
This may help... stackoverflow.com/a/51479636/2836621
– Mark Setchell
Nov 20 at 21:24
This may help... stackoverflow.com/a/51479636/2836621
– Mark Setchell
Nov 20 at 21:24
add a comment |
1 Answer
1
active
oldest
votes
I'd simply use numpy.nonzero
and then find minimum and maximum for each axis.
Script:
import cv2
import numpy as np
img = cv2.imread('blob_in_the_middle.png', cv2.IMREAD_GRAYSCALE)
positions = np.nonzero(img)
top = positions[0].min()
bottom = positions[0].max()
left = positions[1].min()
right = positions[1].max()
output = cv2.rectangle(cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
, (left, top), (right, bottom), (0,255,0), 1)
cv2.imwrite('blob_with_bounds.png', output)
Sample input:
Sample output:
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%2f53401617%2ffinding-first-non-zero-value-in-an-image%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'd simply use numpy.nonzero
and then find minimum and maximum for each axis.
Script:
import cv2
import numpy as np
img = cv2.imread('blob_in_the_middle.png', cv2.IMREAD_GRAYSCALE)
positions = np.nonzero(img)
top = positions[0].min()
bottom = positions[0].max()
left = positions[1].min()
right = positions[1].max()
output = cv2.rectangle(cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
, (left, top), (right, bottom), (0,255,0), 1)
cv2.imwrite('blob_with_bounds.png', output)
Sample input:
Sample output:
add a comment |
I'd simply use numpy.nonzero
and then find minimum and maximum for each axis.
Script:
import cv2
import numpy as np
img = cv2.imread('blob_in_the_middle.png', cv2.IMREAD_GRAYSCALE)
positions = np.nonzero(img)
top = positions[0].min()
bottom = positions[0].max()
left = positions[1].min()
right = positions[1].max()
output = cv2.rectangle(cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
, (left, top), (right, bottom), (0,255,0), 1)
cv2.imwrite('blob_with_bounds.png', output)
Sample input:
Sample output:
add a comment |
I'd simply use numpy.nonzero
and then find minimum and maximum for each axis.
Script:
import cv2
import numpy as np
img = cv2.imread('blob_in_the_middle.png', cv2.IMREAD_GRAYSCALE)
positions = np.nonzero(img)
top = positions[0].min()
bottom = positions[0].max()
left = positions[1].min()
right = positions[1].max()
output = cv2.rectangle(cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
, (left, top), (right, bottom), (0,255,0), 1)
cv2.imwrite('blob_with_bounds.png', output)
Sample input:
Sample output:
I'd simply use numpy.nonzero
and then find minimum and maximum for each axis.
Script:
import cv2
import numpy as np
img = cv2.imread('blob_in_the_middle.png', cv2.IMREAD_GRAYSCALE)
positions = np.nonzero(img)
top = positions[0].min()
bottom = positions[0].max()
left = positions[1].min()
right = positions[1].max()
output = cv2.rectangle(cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
, (left, top), (right, bottom), (0,255,0), 1)
cv2.imwrite('blob_with_bounds.png', output)
Sample input:
Sample output:
answered Nov 20 at 21:30
Dan Mašek
8,74532446
8,74532446
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53401617%2ffinding-first-non-zero-value-in-an-image%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
1
This may help... stackoverflow.com/a/51479636/2836621
– Mark Setchell
Nov 20 at 21:24