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

[pull] master from avinashkranjan:master #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 42 commits into Uncodedtech:master from avinashkranjan:master
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
fb7414a
added bubble-shooter
syamala27 Apr 3, 2021
5074b78
added Uthopianaudio file
syamala27 Apr 3, 2021
0c6086c
Update README.md
syamala27 Apr 3, 2021
de3ecdf
Update README.md
syamala27 Apr 3, 2021
6bb6418
Update README.md
syamala27 Apr 3, 2021
4b50c63
Delete Bubble Shooter Game directory
syamala27 Apr 6, 2021
40ffc48
Added bubble Shooter game
syamala27 Apr 6, 2021
4cdd7ff
Delete Bubble Shooter Game directory
syamala27 Apr 6, 2021
31423e2
Updated the bubble shooter game!
syamala27 Apr 6, 2021
d109536
Add tarot_card_reader.py
Akshu-on-github Apr 8, 2021
4dc0e13
Add README.md
Akshu-on-github Apr 8, 2021
7dd93b3
Modify README.md to fit Template
Akshu-on-github Apr 8, 2021
059d437
Update SCRIPTS.md
Akshu-on-github Apr 8, 2021
9553fe8
Add files via upload
ayush-raj8 Apr 8, 2021
f1e1590
Create README.md
ayush-raj8 Apr 8, 2021
49b3709
Update README.md
ayush-raj8 Apr 8, 2021
9f35c95
Update README.md
ayush-raj8 Apr 8, 2021
e9917ae
Update README.md
ayush-raj8 Apr 8, 2021
71bf35a
Delete main.py
ayush-raj8 Apr 9, 2021
0bc7f6e
Add files via upload
ayush-raj8 Apr 9, 2021
e6e0ee6
Update README.md
ayush-raj8 Apr 9, 2021
bcd09b2
Image viewing
Amit366 Apr 10, 2021
8b4dc47
Update main.py
ayush-raj8 Apr 10, 2021
f4323a1
#868 PDF reoder added
iamakkkhil Apr 10, 2021
900ce45
Readme Updated
iamakkkhil Apr 10, 2021
0940721
#867 Image background subractor added
iamakkkhil Apr 10, 2021
d892fa2
Update
Amit366 Apr 12, 2021
a9316c1
Image viewing gui
Amit366 Apr 12, 2021
6e9a89f
Create Readme.md
syamala27 Apr 12, 2021
399c0e4
Update Readme.md
syamala27 Apr 12, 2021
ba824f5
Update script.py
Amit366 Apr 13, 2021
abcaee5
Undo update
Akshu-on-github Apr 14, 2021
43d6c92
Implement file handling
Akshu-on-github Apr 14, 2021
47b3aa6
Add tarot.txt
Akshu-on-github Apr 14, 2021
1f54536
Add root path
Akshu-on-github Apr 15, 2021
8fbeed0
Modify file handle, update documentation
Akshu-on-github Apr 15, 2021
617478e
Merge pull request #816 from syamala27/master
avinashkranjan Apr 15, 2021
f9557a9
Merge pull request #848 from Akshu-on-github/tarot-reader
avinashkranjan Apr 15, 2021
521e1c4
Merge pull request #852 from ayush-raj8/master
avinashkranjan Apr 15, 2021
9fb8c95
Merge pull request #874 from iamakkkhil/iamakkkhil-patch-1
avinashkranjan Apr 15, 2021
16f8540
Merge pull request #879 from iamakkkhil/iamakkkhil-patch-2
avinashkranjan Apr 15, 2021
47bd8e7
Merge pull request #888 from Amit366/Image
avinashkranjan Apr 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions Image_Background_Subtractor/BG_Subtractor.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import cv2
import numpy as np

# drawn act as a flag variable to see if ROI has been selected or not
drawn = False
ix, iy = -1, -1
# To store ROI values
rectangle = (0, 0, 0, 0)


def open_image(path):
"""
It opens the image when a valid path is given.
:param path: image location path
:return:
"""
img = cv2.imread(path)
dim = (512, 512)
resized_image = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
return resized_image


# mouse callback function
def draw_rectangle(event, x, y, flags, params):
"""
Its a mouse callback function called automatically whenever
any event is happened on the image that is clicking any button on
mouse or keyboard.
"""

global ix, iy, drawn, rectangle
if event == cv2.EVENT_LBUTTONDOWN:
# When you click DOWN with left mouse button
# Then we take note of where that mouse was located
ix, iy = x, y
cv2.circle(img, (ix, iy), 4, (0, 0, 255), -1)

elif event == cv2.EVENT_LBUTTONUP:
# Once you lift the mouse button
# we complete the rectangle ie ROI is selected.
drawn = True
rectangle = (ix, iy, x - ix, y - iy)
print("\nROI Selected Successfully!")


def background_sub(image):
"""
This function:
1. Takes image as input and
2. Asks user to select ROI by dragging mouse pointer.
3. Performs background subtraction.
4. Displaying the updated image.
5. Saving the processed image in your current directory.

GrabCut Algorithm is used
link : https://www.geeksforgeeks.org/python-foreground-extraction-in-an-image-using-grabcut-algorithm/

:param image: Image on which Background subtraction is to be performed.
:return:
"""

# To check if ROI has been selected or not
global drawn

# This names the window so we can reference it
cv2.namedWindow(winname='BG Subractor')
# Connects the mouse button to our callback function
cv2.setMouseCallback('BG Subractor', draw_rectangle)

print("\nSelect ROI from mouse pointer.")

# Creating mask, background and foregound models for Grabcut Algorithm
black_mask = np.zeros(image.shape[:2], np.uint8)
background = np.zeros((1, 65), np.float64)
foreground = np.zeros((1, 65), np.float64)

while True: # Runs forever until we break with Esc key on keyboard

# If ROI is selected
if drawn:
print("\nPerforming Background Subtraction")
# Using Grabcut Algorithm only when ROI is drawn and saved in
# variable named rectangle
cv2.grabCut(image, black_mask, rectangle,
background, foreground,
5, cv2.GC_INIT_WITH_RECT)

# mask with 1 and 4 denotes foreground
# mask with 2 and 0 denotes background so converting the bg pixels into black
mask2 = np.where((black_mask == 2) | (black_mask == 0), 0, 1).astype('uint8')

# multiplying mask2 with original image so that we can get our resultant
image = image * mask2[:, :, np.newaxis]

# For saving the file
cv2.imwrite('Bg_removed.jpg', image)
print(f'\nBg_removed.jpg saved in your current directory!')
print('Great Success!!!')

# Once the processing has been done setting drawn to False
drawn = False

# Shows the resultant image in image window
cv2.imshow('BG Subractor', image)

# Press ESC to exit
if cv2.waitKey(1) & 0xFF == 27:
break

# It closes all windows (just in case you have multiple windows called)
cv2.destroyAllWindows()


if __name__ == '__main__':
loc = input('Enter image path: ')
img = open_image(loc)
background_sub(img)
45 changes: 45 additions & 0 deletions Image_Background_Subtractor/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Image Background Subtraction using OpenCV.

This python script lets you remove background form image by keeping only foreground in focus.
By using [Grabcut Algorithm](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_grabcut/py_grabcut.html) it makes easy to do the task.

We make use of OpenCV, numpy libraries of Python.

This Script:
1. Takes image as input and
2. Asks user to select ROI by dragging mouse pointer.
3. Performs background subtraction.
4. Displaying the updated image.
5. Saving the processed image in your current directory.

![Gif](https://media.giphy.com/media/DZCjZKyNHzsOktHqvI/giphy.gif)

## Setting up:

- Create a virtual environment and activate it.

- Install the requirements

```sh
$ pip install -r requirements.txt
```

## Running the script:

```sh
$ python BG_Subtractor.py
```

1. Enter valid file location/path.
2. Select ROI by using mouse pointer on the window <b>BG Subractor</b>.
3. Then script will process the image.
4. Image <b>Bg_removed.jpg</b> will be stored in your current directory.

![Gif](https://media.giphy.com/media/GVxM1xt46gDS5biPSr/giphy.gif)


## Output:
![Image](https://i.imgur.com/SCf4XRj.jpg)

## Author
[🛡 Akhil Bhalerao 🛡 ](https://linktr.ee/iamakkkhil)
2 changes: 2 additions & 0 deletions Image_Background_Subtractor/requirements.txt
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy==1.20.1
opencv-python==4.5.1.48

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