Object recognition with OpenCV in Python in utility meter images
I wrote a code which finds utility meter measurement result part and crops it. The problem is it does the resize of image. How can I do it without resizing and at the end I need parameters of found part of images, such as width, heith, center x and y.
import numpy as np
import cv2
import sys
from PIL import Image
import imutils
import os.path
if len(sys.argv) != 3:
print ("%s input_file output_file" , sys.argv[0])
sys.exit()
else:
input_file = sys.argv[1]
output_file = sys.argv[2]
if not os.path.isfile(input_file):
print ("No such file '%s'" , input_file)
sys.exit()
img = cv2.imread(input_file)
img = imutils.resize(img, height=500)
## Change to LAB space
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l,a,b = cv2.split(lab)
imglab = np.hstack((l,a,b))
## normalized the a channel to all dynamic range
na = cv2.normalize(a, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
## Threshold to binary
retval, threshed = cv2.threshold(na, thresh = 180, maxval=255, type=cv2.THRESH_BINARY)
## Do morphology
kernel = cv2.getStructuringElement( cv2.MORPH_ELLIPSE , (3,3))
opened = cv2.morphologyEx(threshed, cv2.MORPH_OPEN,kernel)
res = np.hstack((threshed, opened))
## Find contours
_, contours, hierarchy = cv2.findContours(opened, mode=cv2.RETR_LIST, method=cv2.CHAIN_APPROX_SIMPLE)
## Draw Contours
res = img.copy()
cv2.drawContours(res, contours, -1, (255,0,0), 10)
## Filter Contours
for idx, contour in enumerate(contours):
bbox = cv2.boundingRect(contour)
area = bbox[-1]*bbox[-2]
if area < 2150:
continue
(x, y, w, h) = cv2.boundingRect(contour)
print(x, y, w, h)
cv2.rectangle(res, (0, y), (x + w, y + h), (0, 255, 0), 4)
crop_img = img[y:y+h*2, 0:x+w]
(w, h, c) = img.shape
print(w, h)
cv2.imwrite(output_file, crop_img)
Input image is enter image description here
python opencv object-recognition
add a comment |
I wrote a code which finds utility meter measurement result part and crops it. The problem is it does the resize of image. How can I do it without resizing and at the end I need parameters of found part of images, such as width, heith, center x and y.
import numpy as np
import cv2
import sys
from PIL import Image
import imutils
import os.path
if len(sys.argv) != 3:
print ("%s input_file output_file" , sys.argv[0])
sys.exit()
else:
input_file = sys.argv[1]
output_file = sys.argv[2]
if not os.path.isfile(input_file):
print ("No such file '%s'" , input_file)
sys.exit()
img = cv2.imread(input_file)
img = imutils.resize(img, height=500)
## Change to LAB space
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l,a,b = cv2.split(lab)
imglab = np.hstack((l,a,b))
## normalized the a channel to all dynamic range
na = cv2.normalize(a, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
## Threshold to binary
retval, threshed = cv2.threshold(na, thresh = 180, maxval=255, type=cv2.THRESH_BINARY)
## Do morphology
kernel = cv2.getStructuringElement( cv2.MORPH_ELLIPSE , (3,3))
opened = cv2.morphologyEx(threshed, cv2.MORPH_OPEN,kernel)
res = np.hstack((threshed, opened))
## Find contours
_, contours, hierarchy = cv2.findContours(opened, mode=cv2.RETR_LIST, method=cv2.CHAIN_APPROX_SIMPLE)
## Draw Contours
res = img.copy()
cv2.drawContours(res, contours, -1, (255,0,0), 10)
## Filter Contours
for idx, contour in enumerate(contours):
bbox = cv2.boundingRect(contour)
area = bbox[-1]*bbox[-2]
if area < 2150:
continue
(x, y, w, h) = cv2.boundingRect(contour)
print(x, y, w, h)
cv2.rectangle(res, (0, y), (x + w, y + h), (0, 255, 0), 4)
crop_img = img[y:y+h*2, 0:x+w]
(w, h, c) = img.shape
print(w, h)
cv2.imwrite(output_file, crop_img)
Input image is enter image description here
python opencv object-recognition
I don’t understand your question - why is the resize a problem? You wrote the code with the resize, didn’t you? Why did you include the resize?
– barny
Nov 22 '18 at 20:23
Actually this code was made with my friend long time ago. Now I have to do the same without resizing, when I just comment out resizing part It gives another result. I have to do it without resizing, because I have analize the results (e.g intersection over union)
– Perizat Abduraimova
Nov 24 '18 at 13:21
So show the results this code makes you expect - because it is impossible to help someone who says "it doesn't work" without any specific - and edit your code that you are using now into the question and show the results it gives, for the exact same source image, and make clear why you think your code is giving the wrong result (just being "different" is not necessarily wrong, perhaps the original results weren't so correct). Also you might want to ask your friend why the image is resized.
– barny
Nov 24 '18 at 15:09
I think you mean ANALYZE
– barny
Nov 25 '18 at 21:52
add a comment |
I wrote a code which finds utility meter measurement result part and crops it. The problem is it does the resize of image. How can I do it without resizing and at the end I need parameters of found part of images, such as width, heith, center x and y.
import numpy as np
import cv2
import sys
from PIL import Image
import imutils
import os.path
if len(sys.argv) != 3:
print ("%s input_file output_file" , sys.argv[0])
sys.exit()
else:
input_file = sys.argv[1]
output_file = sys.argv[2]
if not os.path.isfile(input_file):
print ("No such file '%s'" , input_file)
sys.exit()
img = cv2.imread(input_file)
img = imutils.resize(img, height=500)
## Change to LAB space
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l,a,b = cv2.split(lab)
imglab = np.hstack((l,a,b))
## normalized the a channel to all dynamic range
na = cv2.normalize(a, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
## Threshold to binary
retval, threshed = cv2.threshold(na, thresh = 180, maxval=255, type=cv2.THRESH_BINARY)
## Do morphology
kernel = cv2.getStructuringElement( cv2.MORPH_ELLIPSE , (3,3))
opened = cv2.morphologyEx(threshed, cv2.MORPH_OPEN,kernel)
res = np.hstack((threshed, opened))
## Find contours
_, contours, hierarchy = cv2.findContours(opened, mode=cv2.RETR_LIST, method=cv2.CHAIN_APPROX_SIMPLE)
## Draw Contours
res = img.copy()
cv2.drawContours(res, contours, -1, (255,0,0), 10)
## Filter Contours
for idx, contour in enumerate(contours):
bbox = cv2.boundingRect(contour)
area = bbox[-1]*bbox[-2]
if area < 2150:
continue
(x, y, w, h) = cv2.boundingRect(contour)
print(x, y, w, h)
cv2.rectangle(res, (0, y), (x + w, y + h), (0, 255, 0), 4)
crop_img = img[y:y+h*2, 0:x+w]
(w, h, c) = img.shape
print(w, h)
cv2.imwrite(output_file, crop_img)
Input image is enter image description here
python opencv object-recognition
I wrote a code which finds utility meter measurement result part and crops it. The problem is it does the resize of image. How can I do it without resizing and at the end I need parameters of found part of images, such as width, heith, center x and y.
import numpy as np
import cv2
import sys
from PIL import Image
import imutils
import os.path
if len(sys.argv) != 3:
print ("%s input_file output_file" , sys.argv[0])
sys.exit()
else:
input_file = sys.argv[1]
output_file = sys.argv[2]
if not os.path.isfile(input_file):
print ("No such file '%s'" , input_file)
sys.exit()
img = cv2.imread(input_file)
img = imutils.resize(img, height=500)
## Change to LAB space
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l,a,b = cv2.split(lab)
imglab = np.hstack((l,a,b))
## normalized the a channel to all dynamic range
na = cv2.normalize(a, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
## Threshold to binary
retval, threshed = cv2.threshold(na, thresh = 180, maxval=255, type=cv2.THRESH_BINARY)
## Do morphology
kernel = cv2.getStructuringElement( cv2.MORPH_ELLIPSE , (3,3))
opened = cv2.morphologyEx(threshed, cv2.MORPH_OPEN,kernel)
res = np.hstack((threshed, opened))
## Find contours
_, contours, hierarchy = cv2.findContours(opened, mode=cv2.RETR_LIST, method=cv2.CHAIN_APPROX_SIMPLE)
## Draw Contours
res = img.copy()
cv2.drawContours(res, contours, -1, (255,0,0), 10)
## Filter Contours
for idx, contour in enumerate(contours):
bbox = cv2.boundingRect(contour)
area = bbox[-1]*bbox[-2]
if area < 2150:
continue
(x, y, w, h) = cv2.boundingRect(contour)
print(x, y, w, h)
cv2.rectangle(res, (0, y), (x + w, y + h), (0, 255, 0), 4)
crop_img = img[y:y+h*2, 0:x+w]
(w, h, c) = img.shape
print(w, h)
cv2.imwrite(output_file, crop_img)
Input image is enter image description here
python opencv object-recognition
python opencv object-recognition
asked Nov 22 '18 at 18:29
Perizat AbduraimovaPerizat Abduraimova
42
42
I don’t understand your question - why is the resize a problem? You wrote the code with the resize, didn’t you? Why did you include the resize?
– barny
Nov 22 '18 at 20:23
Actually this code was made with my friend long time ago. Now I have to do the same without resizing, when I just comment out resizing part It gives another result. I have to do it without resizing, because I have analize the results (e.g intersection over union)
– Perizat Abduraimova
Nov 24 '18 at 13:21
So show the results this code makes you expect - because it is impossible to help someone who says "it doesn't work" without any specific - and edit your code that you are using now into the question and show the results it gives, for the exact same source image, and make clear why you think your code is giving the wrong result (just being "different" is not necessarily wrong, perhaps the original results weren't so correct). Also you might want to ask your friend why the image is resized.
– barny
Nov 24 '18 at 15:09
I think you mean ANALYZE
– barny
Nov 25 '18 at 21:52
add a comment |
I don’t understand your question - why is the resize a problem? You wrote the code with the resize, didn’t you? Why did you include the resize?
– barny
Nov 22 '18 at 20:23
Actually this code was made with my friend long time ago. Now I have to do the same without resizing, when I just comment out resizing part It gives another result. I have to do it without resizing, because I have analize the results (e.g intersection over union)
– Perizat Abduraimova
Nov 24 '18 at 13:21
So show the results this code makes you expect - because it is impossible to help someone who says "it doesn't work" without any specific - and edit your code that you are using now into the question and show the results it gives, for the exact same source image, and make clear why you think your code is giving the wrong result (just being "different" is not necessarily wrong, perhaps the original results weren't so correct). Also you might want to ask your friend why the image is resized.
– barny
Nov 24 '18 at 15:09
I think you mean ANALYZE
– barny
Nov 25 '18 at 21:52
I don’t understand your question - why is the resize a problem? You wrote the code with the resize, didn’t you? Why did you include the resize?
– barny
Nov 22 '18 at 20:23
I don’t understand your question - why is the resize a problem? You wrote the code with the resize, didn’t you? Why did you include the resize?
– barny
Nov 22 '18 at 20:23
Actually this code was made with my friend long time ago. Now I have to do the same without resizing, when I just comment out resizing part It gives another result. I have to do it without resizing, because I have analize the results (e.g intersection over union)
– Perizat Abduraimova
Nov 24 '18 at 13:21
Actually this code was made with my friend long time ago. Now I have to do the same without resizing, when I just comment out resizing part It gives another result. I have to do it without resizing, because I have analize the results (e.g intersection over union)
– Perizat Abduraimova
Nov 24 '18 at 13:21
So show the results this code makes you expect - because it is impossible to help someone who says "it doesn't work" without any specific - and edit your code that you are using now into the question and show the results it gives, for the exact same source image, and make clear why you think your code is giving the wrong result (just being "different" is not necessarily wrong, perhaps the original results weren't so correct). Also you might want to ask your friend why the image is resized.
– barny
Nov 24 '18 at 15:09
So show the results this code makes you expect - because it is impossible to help someone who says "it doesn't work" without any specific - and edit your code that you are using now into the question and show the results it gives, for the exact same source image, and make clear why you think your code is giving the wrong result (just being "different" is not necessarily wrong, perhaps the original results weren't so correct). Also you might want to ask your friend why the image is resized.
– barny
Nov 24 '18 at 15:09
I think you mean ANALYZE
– barny
Nov 25 '18 at 21:52
I think you mean ANALYZE
– barny
Nov 25 '18 at 21:52
add a comment |
0
active
oldest
votes
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%2f53436476%2fobject-recognition-with-opencv-in-python-in-utility-meter-images%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53436476%2fobject-recognition-with-opencv-in-python-in-utility-meter-images%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
I don’t understand your question - why is the resize a problem? You wrote the code with the resize, didn’t you? Why did you include the resize?
– barny
Nov 22 '18 at 20:23
Actually this code was made with my friend long time ago. Now I have to do the same without resizing, when I just comment out resizing part It gives another result. I have to do it without resizing, because I have analize the results (e.g intersection over union)
– Perizat Abduraimova
Nov 24 '18 at 13:21
So show the results this code makes you expect - because it is impossible to help someone who says "it doesn't work" without any specific - and edit your code that you are using now into the question and show the results it gives, for the exact same source image, and make clear why you think your code is giving the wrong result (just being "different" is not necessarily wrong, perhaps the original results weren't so correct). Also you might want to ask your friend why the image is resized.
– barny
Nov 24 '18 at 15:09
I think you mean ANALYZE
– barny
Nov 25 '18 at 21:52