Remove elements attached to image borders


Mostafa 2000

I'm using OpenCV to detect pneumonia in a chest x-ray using image processing, so I need to remove the additional area of ​​the image border to get only the lungs, can anyone help me code in python?

This picture explains the picture I want after applying these methods : resizing, histogram equalization, Otsu thresholding and inverse binary thresholding, morphological process (on and off)

This is the original image

fmw42

This is how I approach the problem in Python/OpenCV. Add a white border around it, fill it with black to replace the white, and remove the excess border.

enter:

enter image description here


import cv2
import numpy as np

# read image
img = cv2.imread('lungs.jpg')
h, w = img.shape[:2]

# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# add 1 pixel white border all around
pad = cv2.copyMakeBorder(gray, 1,1,1,1, cv2.BORDER_CONSTANT, value=255)
h, w = pad.shape

# create zeros mask 2 pixels larger in each dimension
mask = np.zeros([h + 2, w + 2], np.uint8)

# floodfill outer white border with black
img_floodfill = cv2.floodFill(pad, mask, (0,0), 0, (5), (0), flags=8)[1]

# remove border
img_floodfill = img_floodfill[1:h-1, 1:w-1]    

# save cropped image
cv2.imwrite('lungs_floodfilled.png',img_floodfill)

# show the images
cv2.imshow("img_floodfill", img_floodfill)
cv2.waitKey(0)
cv2.destroyAllWindows()

enter image description here

Related


Remove image borders with JavaScript

Grosk I have an image with a border that should not work in Chrome, Safari, IE and Opera, but not in Firefox. I'm assuming it's a browser default issue, but I'm working on removing it because it's running as an AB split test and I can't modify the basic html/c

Remove image borders with JavaScript

Grosk I have an image with a border that should not work in Chrome, Safari, IE and Opera, but not in Firefox. I'm assuming it's a browser default issue, but I'm working on removing it because it's running as an AB split test and I can't modify the basic html/c

How to remove attached elements using delete

username I have a simple code with 2 grids. A div contains a button called "Add". When clicked, additional buttons will be added in the second div. When you click any button in the second div it may delete itself. As you can see in the code below, I am using a

How to remove attached elements using delete

username I have a simple code with 2 grids. A div contains a button called "Add". When clicked, additional buttons will be added in the second div. When you click any button in the second div it may delete itself. As you can see in the code below, I am using a

Remove attached image from jQuery dialog title

Jane S I've looked into various solutions on StackOverflow but haven't had any success getting them to work. I can easily add an image to the dialog, but various attempts to remove the image have failed, so I get multiple images. Note that I don't want it to a

Remove attached image from jQuery dialog title

Jane S I've looked into various solutions on StackOverflow but haven't had any success getting them to work. I can easily add an image to the dialog, but various attempts to remove the image have failed, so I get multiple images. Note that I don't want it to a

How to remove image borders when exporting from Excel?

DT Here is an image of my code exporting from excel to file For Each oShape In ActiveSheet.Shapes strImageName = oShape.TopLeftCell.Row & "_" & oShape.TopLeftCell.Column If oShape.Type = msoPicture Then oShape.Select 'Picture format ini

How to remove image borders when exporting from Excel?

DT Here is an image of my code exporting from excel to file For Each oShape In ActiveSheet.Shapes strImageName = oShape.TopLeftCell.Row & "_" & oShape.TopLeftCell.Column If oShape.Type = msoPicture Then oShape.Select 'Picture format ini

How to remove image borders when exporting from Excel?

DT Here is an image of my code exporting from excel to file For Each oShape In ActiveSheet.Shapes strImageName = oShape.TopLeftCell.Row & "_" & oShape.TopLeftCell.Column If oShape.Type = msoPicture Then oShape.Select 'Picture format ini

How can I remove the Altair yaxis of the attached image?

ZKK How to remove the blue part? import altair as alt from vega_datasets import data iris = data.iris() alt.Chart(iris).mark_point().encode( x='petalWidth', y='petalLength', color='species' ).configure_axis( grid=False ).configure_view( s

How can I remove the Altair yaxis of the attached image?

ZKK How to remove the blue part? import altair as alt from vega_datasets import data iris = data.iris() alt.Chart(iris).mark_point().encode( x='petalWidth', y='petalLength', color='species' ).configure_axis( grid=False ).configure_view( s

How can I remove the Altair yaxis of the attached image?

ZKK How to remove the blue part? import altair as alt from vega_datasets import data iris = data.iris() alt.Chart(iris).mark_point().encode( x='petalWidth', y='petalLength', color='species' ).configure_axis( grid=False ).configure_view( s

How can I remove the Altair yaxis of the attached image?

ZKK How to remove the blue part? import altair as alt from vega_datasets import data iris = data.iris() alt.Chart(iris).mark_point().encode( x='petalWidth', y='petalLength', color='species' ).configure_axis( grid=False ).configure_view( s

How can I remove the Altair yaxis of the attached image?

ZKK How to remove the blue part? import altair as alt from vega_datasets import data iris = data.iris() alt.Chart(iris).mark_point().encode( x='petalWidth', y='petalLength', color='species' ).configure_axis( grid=False ).configure_view( s

How can I remove the Altair yaxis of the attached image?

ZKK How to remove the blue part? import altair as alt from vega_datasets import data iris = data.iris() alt.Chart(iris).mark_point().encode( x='petalWidth', y='petalLength', color='species' ).configure_axis( grid=False ).configure_view( s

How can I remove the Altair yaxis of the attached image?

ZKK How to remove the blue part? import altair as alt from vega_datasets import data iris = data.iris() alt.Chart(iris).mark_point().encode( x='petalWidth', y='petalLength', color='species' ).configure_axis( grid=False ).configure_view( s

How can I remove the Altair yaxis of the attached image?

ZKK How to remove the blue part? import altair as alt from vega_datasets import data iris = data.iris() alt.Chart(iris).mark_point().encode( x='petalWidth', y='petalLength', color='species' ).configure_axis( grid=False ).configure_view( s

Multiple borders with borders around the image

Ronnie I want to create multiple borders around my image with some padding as shown below. I prefer to do this with just CSS, but I don't know if this is possible. When I googled I only found similar examples using box shadows to have multiple borders around o

Multiple borders with borders around the image

Ronnie I want to create multiple borders around my image with some padding as shown below. I prefer to do this with just CSS, but I don't know if this is possible. When I googled I only found similar examples using box shadows to have multiple borders around o