forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
[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
Merged
Changes from 2 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
fb7414a
added bubble-shooter
syamala27 5074b78
added Uthopianaudio file
syamala27 0c6086c
Update README.md
syamala27 de3ecdf
Update README.md
syamala27 6bb6418
Update README.md
syamala27 4b50c63
Delete Bubble Shooter Game directory
syamala27 40ffc48
Added bubble Shooter game
syamala27 4cdd7ff
Delete Bubble Shooter Game directory
syamala27 31423e2
Updated the bubble shooter game!
syamala27 d109536
Add tarot_card_reader.py
Akshu-on-github 4dc0e13
Add README.md
Akshu-on-github 7dd93b3
Modify README.md to fit Template
Akshu-on-github 059d437
Update SCRIPTS.md
Akshu-on-github 9553fe8
Add files via upload
ayush-raj8 f1e1590
Create README.md
ayush-raj8 49b3709
Update README.md
ayush-raj8 9f35c95
Update README.md
ayush-raj8 e9917ae
Update README.md
ayush-raj8 71bf35a
Delete main.py
ayush-raj8 0bc7f6e
Add files via upload
ayush-raj8 e6e0ee6
Update README.md
ayush-raj8 bcd09b2
Image viewing
Amit366 8b4dc47
Update main.py
ayush-raj8 f4323a1
#868 PDF reoder added
iamakkkhil 900ce45
Readme Updated
iamakkkhil 0940721
#867 Image background subractor added
iamakkkhil d892fa2
Update
Amit366 a9316c1
Image viewing gui
Amit366 6e9a89f
Create Readme.md
syamala27 399c0e4
Update Readme.md
syamala27 ba824f5
Update script.py
Amit366 abcaee5
Undo update
Akshu-on-github 43d6c92
Implement file handling
Akshu-on-github 47b3aa6
Add tarot.txt
Akshu-on-github 1f54536
Add root path
Akshu-on-github 8fbeed0
Modify file handle, update documentation
Akshu-on-github 617478e
Merge pull request #816 from syamala27/master
avinashkranjan f9557a9
Merge pull request #848 from Akshu-on-github/tarot-reader
avinashkranjan 521e1c4
Merge pull request #852 from ayush-raj8/master
avinashkranjan 9fb8c95
Merge pull request #874 from iamakkkhil/iamakkkhil-patch-1
avinashkranjan 16f8540
Merge pull request #879 from iamakkkhil/iamakkkhil-patch-2
avinashkranjan 47bd8e7
Merge pull request #888 from Amit366/Image
avinashkranjan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
Image_Background_Subtractor/BG_Subtractor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
 | ||
|
||
## 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. | ||
|
||
 | ||
|
||
|
||
## Output: | ||
 | ||
|
||
## Author | ||
[🛡 Akhil Bhalerao 🛡 ](https://linktr.ee/iamakkkhil) |
2 changes: 2 additions & 0 deletions
Image_Background_Subtractor/requirements.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
numpy==1.20.1 | ||
opencv-python==4.5.1.48 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.