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










share|improve this question






















  • 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

















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










share|improve this question






















  • 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















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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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




















  • 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



















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',
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
});


}
});














 

draft saved


draft discarded


















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





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()