GithubHelp home page GithubHelp logo

ioxua / cspacefilterpython Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alkasm/colorfilters

0.0 1.0 0.0 78 KB

Python script to quickly test colorspace thresholding values on color images.

License: MIT License

Python 100.00%

cspacefilterpython's Introduction

cspaceFilter

Python module to quickly test colorspace thresholding values on color images.

files

.gitignore          ignore all images other than the example `lane.jpg`
LICENSE             MIT License
README.md           this file
cspaceSliders.py    module containing FilterWindow class that displays the thresholded image with sliders
example.py          an example of how you might use the functions
lane.jpg            an included example image; try filtering out the sky or obtaining the lane lines

use

import cv2
from cspaceSliders import FilterWindow
image = cv2.imread('lane.jpg')
window = FilterWindow('Filter Window', image)
window.show(verbose=True)

While the window is showing, there is a number of keyboard shortcuts to change the thresholding operations and to modify what's being displayed:

Press 1 thru 8 (inclusive) to switch between colorspaces.
Colorspaces: 1: BGR, 2: HSV, 3: HLS 4: Lab, 5: Luv, 6: YCrCb, 7: XYZ, 8: Grayscale
Press m to switch between displaying mask and applied mask
Press b to print current lower and upper bounds
Press s to save the currently displayed image
Press q or esc to close the window

The class properties are updated during .show() and if you want to grab them after closing the window (or during window display) you can get the current colorspace, upper and lower bounds, mask, and applied mask with:

colorspace = window.colorspace
lowerb, upperb = window.bounds
mask = window.mask
applied_mask = window.applied_mask

Lastly, the .show() method has an optional argument, verbose which is False by default. A value of True will print out every time the displayed image is switched from mask to applied_mask, will print every time the colorspace is changed, and will print the colorspace and bounds when the image is closed. A value of False still prints instructions when the image is first displayed, but doesn't print any more after that (unless b is pressed to print the current bounds).

web app

With another friend, I am currently developing a web application to do the same thing but through your browser (locally as well as on the internet) for a nicer interface and easy use for all who might not be using Python. Additionally trackbars sometimes get placed in a weird order (see known issues below), so the slider script could be annoying to use. Finally, OpenCV does not have a robust UI at all---it only has the bare minimum. The web app will enable much better interactivity. You can check out the current working version here. The interface isn't finished yet, but it works.

known issues

Depending on your build of OpenCV, the sliders (trackbars) get placed in a weird order. This is a known bug. For those with the issue, ordering of them is somehow dependant on their name. I've added a couple spaces to some of my trackbar names to get them in a satisfactory order on my machine---I have no idea if others with this issue will have them in my same order or not. If not, at least the trackbars are labeled.

If the image height is larger than your screen resolution (minus the trackbar size, and window borders), then the trackbars will be displayed on top of your image. Simply resize the image before sending it through, and double check that they work on the larger image after by using the lowerb and upperb return values with cv2.inRange(image, lowerb, upperb).

possible updates

Morphological operations such as erosion, dilation, opening, or closing are often done in-tandem with colorspace range thresholding to remove spurious bits. It would be nice to include views of these steps along with the thresholding since they also have tunable parameters (height and width of the kernel, number of iterations). However, it seems difficult to implement as these are often done in different orders. This might be easier to complete for a browser-based implementation.

contribute

Please report any errors you find, and open a pull request if you fix. Alternatively, if you would like to port these scripts to another language which uses OpenCV, please feel free to do so.

Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.