Python - OpenCV - Binarize To Isolate Object Which is Same Color as Background
up vote
1
down vote
favorite
I need to isolate the cardboard target in the image below and binarize it, so that the target is white and the background black. Normally, this is not a problem, but the background is almost the exact same color as the target.
Attempts:
# LOAD IMAGE
img_filepath = 'real_6.png'
img = cv2.imread( img_filepath )
rgb_img = img[:,:,::-1]
plt.imshow( rgb_img )
plt.title('ORIGINAL')
plt.show()
img_gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )
# SMOOTH
blur_kernel = np.ones((5,5),np.float32)/30
blur_img = cv2.filter2D( rgb_img, -1, blur_kernel )
# THRESHOLD
lower_color_rng = np.array( [100,50,100] )
upper_color_rng = np.array( [255,255,255] )
target_keyholes_img = cv2.inRange( blur_img, lower_color_rng, upper_color_rng )
plt.imshow( target_keyholes_img, cmap='gray' )
plt.title( 'THRESHOLD' )
plt.show()
Attempted Image Extraction
How can I use OpenCV in Python 3 to binarize this image?
Original Image
python-3.x image opencv signal-processing
add a comment |
up vote
1
down vote
favorite
I need to isolate the cardboard target in the image below and binarize it, so that the target is white and the background black. Normally, this is not a problem, but the background is almost the exact same color as the target.
Attempts:
# LOAD IMAGE
img_filepath = 'real_6.png'
img = cv2.imread( img_filepath )
rgb_img = img[:,:,::-1]
plt.imshow( rgb_img )
plt.title('ORIGINAL')
plt.show()
img_gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )
# SMOOTH
blur_kernel = np.ones((5,5),np.float32)/30
blur_img = cv2.filter2D( rgb_img, -1, blur_kernel )
# THRESHOLD
lower_color_rng = np.array( [100,50,100] )
upper_color_rng = np.array( [255,255,255] )
target_keyholes_img = cv2.inRange( blur_img, lower_color_rng, upper_color_rng )
plt.imshow( target_keyholes_img, cmap='gray' )
plt.title( 'THRESHOLD' )
plt.show()
Attempted Image Extraction
How can I use OpenCV in Python 3 to binarize this image?
Original Image
python-3.x image opencv signal-processing
you can try grabcut opencv algorithm for foreground extraction
– Devashish Prasad
Nov 5 at 7:44
If the color of your object is slightly different (as in the example image you provided), you can try to find the correct threshold that divides it from the background. If the color is in fact the same as your background, there is no way to isolate it.
– T A
Nov 5 at 16:05
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I need to isolate the cardboard target in the image below and binarize it, so that the target is white and the background black. Normally, this is not a problem, but the background is almost the exact same color as the target.
Attempts:
# LOAD IMAGE
img_filepath = 'real_6.png'
img = cv2.imread( img_filepath )
rgb_img = img[:,:,::-1]
plt.imshow( rgb_img )
plt.title('ORIGINAL')
plt.show()
img_gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )
# SMOOTH
blur_kernel = np.ones((5,5),np.float32)/30
blur_img = cv2.filter2D( rgb_img, -1, blur_kernel )
# THRESHOLD
lower_color_rng = np.array( [100,50,100] )
upper_color_rng = np.array( [255,255,255] )
target_keyholes_img = cv2.inRange( blur_img, lower_color_rng, upper_color_rng )
plt.imshow( target_keyholes_img, cmap='gray' )
plt.title( 'THRESHOLD' )
plt.show()
Attempted Image Extraction
How can I use OpenCV in Python 3 to binarize this image?
Original Image
python-3.x image opencv signal-processing
I need to isolate the cardboard target in the image below and binarize it, so that the target is white and the background black. Normally, this is not a problem, but the background is almost the exact same color as the target.
Attempts:
# LOAD IMAGE
img_filepath = 'real_6.png'
img = cv2.imread( img_filepath )
rgb_img = img[:,:,::-1]
plt.imshow( rgb_img )
plt.title('ORIGINAL')
plt.show()
img_gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )
# SMOOTH
blur_kernel = np.ones((5,5),np.float32)/30
blur_img = cv2.filter2D( rgb_img, -1, blur_kernel )
# THRESHOLD
lower_color_rng = np.array( [100,50,100] )
upper_color_rng = np.array( [255,255,255] )
target_keyholes_img = cv2.inRange( blur_img, lower_color_rng, upper_color_rng )
plt.imshow( target_keyholes_img, cmap='gray' )
plt.title( 'THRESHOLD' )
plt.show()
Attempted Image Extraction
How can I use OpenCV in Python 3 to binarize this image?
Original Image
python-3.x image opencv signal-processing
python-3.x image opencv signal-processing
asked Nov 5 at 1:49
Alexander Falinsky
134
134
you can try grabcut opencv algorithm for foreground extraction
– Devashish Prasad
Nov 5 at 7:44
If the color of your object is slightly different (as in the example image you provided), you can try to find the correct threshold that divides it from the background. If the color is in fact the same as your background, there is no way to isolate it.
– T A
Nov 5 at 16:05
add a comment |
you can try grabcut opencv algorithm for foreground extraction
– Devashish Prasad
Nov 5 at 7:44
If the color of your object is slightly different (as in the example image you provided), you can try to find the correct threshold that divides it from the background. If the color is in fact the same as your background, there is no way to isolate it.
– T A
Nov 5 at 16:05
you can try grabcut opencv algorithm for foreground extraction
– Devashish Prasad
Nov 5 at 7:44
you can try grabcut opencv algorithm for foreground extraction
– Devashish Prasad
Nov 5 at 7:44
If the color of your object is slightly different (as in the example image you provided), you can try to find the correct threshold that divides it from the background. If the color is in fact the same as your background, there is no way to isolate it.
– T A
Nov 5 at 16:05
If the color of your object is slightly different (as in the example image you provided), you can try to find the correct threshold that divides it from the background. If the color is in fact the same as your background, there is no way to isolate it.
– T A
Nov 5 at 16:05
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147320%2fpython-opencv-binarize-to-isolate-object-which-is-same-color-as-background%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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
you can try grabcut opencv algorithm for foreground extraction
– Devashish Prasad
Nov 5 at 7:44
If the color of your object is slightly different (as in the example image you provided), you can try to find the correct threshold that divides it from the background. If the color is in fact the same as your background, there is no way to isolate it.
– T A
Nov 5 at 16:05