Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d68b1a9

Browse files
Merge pull request #1074 from Khushi-sharma07/main
Image Background Remover
2 parents 0b50533 + 1da2750 commit d68b1a9

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed
478 KB
Loading[フレーム]
235 KB
Loading[フレーム]
63.6 KB
Loading[フレーム]
6.23 KB
Loading[フレーム]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Image Background Remover
2+
3+
# Aim
4+
This project helps in removing the background of an image and just focuses on foreground and changes the background to yellow.
5+
6+
# Purpose
7+
In this project a normal image gets it background removed using a single script.
8+
9+
# Short description of package/script
10+
>>This project contains a script bgremover.py which helps to remove the background of an image.
11+
>>It uses Open CV library to manipulate pixels of the image
12+
>>Numpy is used because Open CV stores images in form of numpy array and it can be manipulated easily by performing numpy operations.
13+
14+
# Workflow of the Project
15+
User will enter the name of the file present in the media folder whose background is to be removed then the program will process the image and remove the background and will only focus on its foreground. Then simply the converted file will be opened and displayed with the changes.
16+
17+
# Detailed Setup instructions
18+
The first step is to install python and fulfill all the requirements mentioned in "requirements.txt". Then copy your images to the Media folder. The extension of image should be .jpg to get the best results.
19+
After running the program you will get the image converted as per your need and will be visible to you on the screen as output.
20+
21+
# Compilation Steps
22+
In this we are using cv2 and numpy to read the images and then store it in the form of array. Then with the help of edge detection, contours and blurring the image the pixels are properly set and the new image is displayed on the screen and the focus is just on the foreground.
23+
24+
# Conclusion
25+
This image processing script helps the user to instantly give focus to just the foreground of an image and removes it background.
26+
27+
# Screenshot
28+
29+
![Image Background Remover](Media/Screenshot1.jpg)
30+
31+
32+
![Image Background Remover](Media/Screenshot2.jpg)
33+
34+
# Author
35+
36+
Khushi Sharma
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import cv2
2+
import numpy as np
3+
4+
BLUR = 21
5+
CANNY_THRESH_1 = 10
6+
CANNY_THRESH_2 = 200
7+
MASK_DILATE_ITER = 10
8+
MASK_ERODE_ITER = 10
9+
MASK_COLOR = (0,1.0,1.0) #BGR
10+
11+
12+
#Read image
13+
filename = input("Please Enter File Name : ") # Asking Image name
14+
newfilename = "Media/" + filename
15+
img = cv2.imread(newfilename)
16+
17+
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
18+
19+
#Edge detection
20+
edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2)
21+
edges = cv2.dilate(edges, None)
22+
edges = cv2.erode(edges, None)
23+
24+
#Count the contours in edges, sort by area
25+
contour_info = []
26+
contours,_=cv2.findContours(edges,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
27+
28+
for c in contours:
29+
contour_info.append((c,cv2.isContourConvex(c),cv2.contourArea(c)))
30+
contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
31+
max_contour = contour_info[0]
32+
33+
# Mask is black, polygon is white
34+
mask = np.zeros(edges.shape)
35+
cv2.fillConvexPoly(mask, max_contour[0], (255))
36+
37+
#Smooth mask and then blur
38+
mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER)
39+
mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER)
40+
mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0)
41+
mask_stack=np.dstack([mask]*3)
42+
43+
mask_stack =mask_stack.astype('float32') / 255.0
44+
img=img.astype('float32')/255.0
45+
46+
masked = (mask_stack * img) + ((1-mask_stack) * MASK_COLOR)
47+
masked = (masked * 255).astype('uint8')
48+
49+
cv2.imshow('pic',masked)
50+
cv2.waitKey()
51+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /