How do I select only the area inside contours?












1














I'm implementing a Foreground Extraction algorithm in Python which creates a mask and contours of the foreground of the image.



However, I'm working with SVS files (tif files with less properties) and after extracting the foreground and I still need to tile it because my image has gigapixel resolution. I'm using openCV to handle these images.



This is my getContours function:



def getContours(oslIm, level=2):
"""
Loads the requested level from oslIm and returns identified contours in the slide.
:param oslIm: openslide image
:param level: level from which to load the data (usually 2)
:return: contours
"""

image_array = np.asarray(oslIm.read_region((1, 1), level, oslIm.level_dimensions[level]), dtype=np.uint8)
image = image_array[:, :, 0:3]
mask = segment(image)
_, contours, _ = cv2.findContours(mask, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)

return contours


I read my image at the desired level, I extract the mask of the foreground and then the contours.



This is the process:



Input Image:



enter image description here



Mask:



enter image description here



Final Image:



enter image description here



Everything is good, I have the contours and the final image.
But as I said before I want to crop my image so I can apply my



sample_and_store_patches_by_row(
file_name,
pixel_overlap,
patch_size=512,
level=17,
)


and tile my image in 512x512 images. For that I need to get inside the contours and tile the whats inside of it.



I'm struggling with this for days, any help would be highly appreciated.



Thank you










share|improve this question






















  • So you want to iterate inside the contour row by row ?
    – ZdaR
    Nov 13 at 5:38










  • I want to do something like this: original_image > generate mask > get contours oritinal_image * contours = image_to_tile generate_patches(image_to_tile)
    – Luís Costa
    Nov 13 at 5:41










  • Please explain this image_to_tile functionality? What is the expected output ?
    – ZdaR
    Nov 13 at 6:20










  • image_to_tile is the original image without the background. I want to tile the foreground of the original svs image
    – Luís Costa
    Nov 13 at 19:33










  • Can you please provide sample output? I am really unable to get your expected output here. Do you want the background to be white instead of black as in current output ?
    – ZdaR
    Nov 14 at 4:09
















1














I'm implementing a Foreground Extraction algorithm in Python which creates a mask and contours of the foreground of the image.



However, I'm working with SVS files (tif files with less properties) and after extracting the foreground and I still need to tile it because my image has gigapixel resolution. I'm using openCV to handle these images.



This is my getContours function:



def getContours(oslIm, level=2):
"""
Loads the requested level from oslIm and returns identified contours in the slide.
:param oslIm: openslide image
:param level: level from which to load the data (usually 2)
:return: contours
"""

image_array = np.asarray(oslIm.read_region((1, 1), level, oslIm.level_dimensions[level]), dtype=np.uint8)
image = image_array[:, :, 0:3]
mask = segment(image)
_, contours, _ = cv2.findContours(mask, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)

return contours


I read my image at the desired level, I extract the mask of the foreground and then the contours.



This is the process:



Input Image:



enter image description here



Mask:



enter image description here



Final Image:



enter image description here



Everything is good, I have the contours and the final image.
But as I said before I want to crop my image so I can apply my



sample_and_store_patches_by_row(
file_name,
pixel_overlap,
patch_size=512,
level=17,
)


and tile my image in 512x512 images. For that I need to get inside the contours and tile the whats inside of it.



I'm struggling with this for days, any help would be highly appreciated.



Thank you










share|improve this question






















  • So you want to iterate inside the contour row by row ?
    – ZdaR
    Nov 13 at 5:38










  • I want to do something like this: original_image > generate mask > get contours oritinal_image * contours = image_to_tile generate_patches(image_to_tile)
    – Luís Costa
    Nov 13 at 5:41










  • Please explain this image_to_tile functionality? What is the expected output ?
    – ZdaR
    Nov 13 at 6:20










  • image_to_tile is the original image without the background. I want to tile the foreground of the original svs image
    – Luís Costa
    Nov 13 at 19:33










  • Can you please provide sample output? I am really unable to get your expected output here. Do you want the background to be white instead of black as in current output ?
    – ZdaR
    Nov 14 at 4:09














1












1








1







I'm implementing a Foreground Extraction algorithm in Python which creates a mask and contours of the foreground of the image.



However, I'm working with SVS files (tif files with less properties) and after extracting the foreground and I still need to tile it because my image has gigapixel resolution. I'm using openCV to handle these images.



This is my getContours function:



def getContours(oslIm, level=2):
"""
Loads the requested level from oslIm and returns identified contours in the slide.
:param oslIm: openslide image
:param level: level from which to load the data (usually 2)
:return: contours
"""

image_array = np.asarray(oslIm.read_region((1, 1), level, oslIm.level_dimensions[level]), dtype=np.uint8)
image = image_array[:, :, 0:3]
mask = segment(image)
_, contours, _ = cv2.findContours(mask, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)

return contours


I read my image at the desired level, I extract the mask of the foreground and then the contours.



This is the process:



Input Image:



enter image description here



Mask:



enter image description here



Final Image:



enter image description here



Everything is good, I have the contours and the final image.
But as I said before I want to crop my image so I can apply my



sample_and_store_patches_by_row(
file_name,
pixel_overlap,
patch_size=512,
level=17,
)


and tile my image in 512x512 images. For that I need to get inside the contours and tile the whats inside of it.



I'm struggling with this for days, any help would be highly appreciated.



Thank you










share|improve this question













I'm implementing a Foreground Extraction algorithm in Python which creates a mask and contours of the foreground of the image.



However, I'm working with SVS files (tif files with less properties) and after extracting the foreground and I still need to tile it because my image has gigapixel resolution. I'm using openCV to handle these images.



This is my getContours function:



def getContours(oslIm, level=2):
"""
Loads the requested level from oslIm and returns identified contours in the slide.
:param oslIm: openslide image
:param level: level from which to load the data (usually 2)
:return: contours
"""

image_array = np.asarray(oslIm.read_region((1, 1), level, oslIm.level_dimensions[level]), dtype=np.uint8)
image = image_array[:, :, 0:3]
mask = segment(image)
_, contours, _ = cv2.findContours(mask, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)

return contours


I read my image at the desired level, I extract the mask of the foreground and then the contours.



This is the process:



Input Image:



enter image description here



Mask:



enter image description here



Final Image:



enter image description here



Everything is good, I have the contours and the final image.
But as I said before I want to crop my image so I can apply my



sample_and_store_patches_by_row(
file_name,
pixel_overlap,
patch_size=512,
level=17,
)


and tile my image in 512x512 images. For that I need to get inside the contours and tile the whats inside of it.



I'm struggling with this for days, any help would be highly appreciated.



Thank you







python opencv






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 at 5:33









Luís Costa

283217




283217












  • So you want to iterate inside the contour row by row ?
    – ZdaR
    Nov 13 at 5:38










  • I want to do something like this: original_image > generate mask > get contours oritinal_image * contours = image_to_tile generate_patches(image_to_tile)
    – Luís Costa
    Nov 13 at 5:41










  • Please explain this image_to_tile functionality? What is the expected output ?
    – ZdaR
    Nov 13 at 6:20










  • image_to_tile is the original image without the background. I want to tile the foreground of the original svs image
    – Luís Costa
    Nov 13 at 19:33










  • Can you please provide sample output? I am really unable to get your expected output here. Do you want the background to be white instead of black as in current output ?
    – ZdaR
    Nov 14 at 4:09


















  • So you want to iterate inside the contour row by row ?
    – ZdaR
    Nov 13 at 5:38










  • I want to do something like this: original_image > generate mask > get contours oritinal_image * contours = image_to_tile generate_patches(image_to_tile)
    – Luís Costa
    Nov 13 at 5:41










  • Please explain this image_to_tile functionality? What is the expected output ?
    – ZdaR
    Nov 13 at 6:20










  • image_to_tile is the original image without the background. I want to tile the foreground of the original svs image
    – Luís Costa
    Nov 13 at 19:33










  • Can you please provide sample output? I am really unable to get your expected output here. Do you want the background to be white instead of black as in current output ?
    – ZdaR
    Nov 14 at 4:09
















So you want to iterate inside the contour row by row ?
– ZdaR
Nov 13 at 5:38




So you want to iterate inside the contour row by row ?
– ZdaR
Nov 13 at 5:38












I want to do something like this: original_image > generate mask > get contours oritinal_image * contours = image_to_tile generate_patches(image_to_tile)
– Luís Costa
Nov 13 at 5:41




I want to do something like this: original_image > generate mask > get contours oritinal_image * contours = image_to_tile generate_patches(image_to_tile)
– Luís Costa
Nov 13 at 5:41












Please explain this image_to_tile functionality? What is the expected output ?
– ZdaR
Nov 13 at 6:20




Please explain this image_to_tile functionality? What is the expected output ?
– ZdaR
Nov 13 at 6:20












image_to_tile is the original image without the background. I want to tile the foreground of the original svs image
– Luís Costa
Nov 13 at 19:33




image_to_tile is the original image without the background. I want to tile the foreground of the original svs image
– Luís Costa
Nov 13 at 19:33












Can you please provide sample output? I am really unable to get your expected output here. Do you want the background to be white instead of black as in current output ?
– ZdaR
Nov 14 at 4:09




Can you please provide sample output? I am really unable to get your expected output here. Do you want the background to be white instead of black as in current output ?
– ZdaR
Nov 14 at 4:09

















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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274439%2fhow-do-i-select-only-the-area-inside-contours%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274439%2fhow-do-i-select-only-the-area-inside-contours%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)