I work with a huge library of components (step files) that are currently used in various products. My goal is to identify parts with great similarity in order to unify them. At the moment I can think of two solutions:
Compare certain properties of the 3D data with a suitable python library. E.g. identify parts with similar volume and dimensions.
Convert step files to JPG and compare the images with one of the many image processing libraries.
Both have their pitfalls.
Is there a library that can handle step files or do you know a better way to solve the problem?
You are underestimating the complexity of this project. Once the STEP geometry is loaded, taking dimensions on it (apart from bounding box extents) can be really cumbersome. Very different parts can have the same volume and comparing bitmaps you completely ignore the hidden part of the geometry.
Related
I have many stl CAD files for 3D printing and I was wondering how I can manipulate an individual CAD file by making a simple change programatically. Like if I want to double the dimensions of everything in the CAD file, how could I accomplish this in python? I could convert it to gcode if that makes things simpler.
I have come across FreeCad but I am not sure if this is the easiest and best way of programmatically altering CAD files. I also wanted an option that allows for freedom of manipulation beyond just size, maybe also seeing how I can programmatically alter the shape of the CAD file.
Well to be precise, STL files are even not meshes, but triangles soups. Which means that triangles are even not connected to each other (well they need to be adjacent to be used by software generatif g-code, but no obligation else)
just for clarification: g-code format is just a sequence of instructions for the machines (3d-printers, etc) so it's very good for printers, but also certainly the worst format to manipulate geometry.
one solution
you can use the python pymadcad module to deal with STL files
So for simple transformation operations (such as scale, translation etc)
from madcad import *
part = read('path/to/your_file.stl')
transformed = part.transform(mat3(2)) # scale by a factor 2 (diagonal matrix of 2)
write(transformed, 'path/to/output_file.stl')
If you need more complex operations (such as booleans, chamfers, etc) you will need to make the triangle connected again before the desired operations.
from madcad import *
part = read('path/to/your_file.stl')
part.mergeclose() # merge points at the same location
# your desired operations (example)
transformed = union(part, icosphere(vec3(1,1,1), 3.)) # add a sphere a diameter 3
write(transformed, 'path/to/output_file.stl')
note
pymadcad is internally using numpy-stl to import stl files, if you need only basic manipulations you can also only install that one
STL files are meshes. Their exterior is defined by triangulated surfaces. Meshes are notoriously difficult to modify after the fact. That's why you always want to save your designs in the native format and also in an open solid format like .STP or .IGES.
That said, simple scaling of a mesh is pretty easy and can be done trivially in FreeCAD.
You can do it in the gui in the mesh design workbench or through python. Here's a post about that:
https://forum.freecadweb.org/viewtopic.php?t=9109#p74047
FreeCAD also has some tools to convert a mesh into a solid. Results are usually less than spectacular but might be good enough depending on your needs. Search on the FreeCAD forum for posts about converting or look here: https://wiki.freecadweb.org/FreeCAD_and_Mesh_Import
Gcode doesn't say anything about the model itself. It specifies where a tool should move so it's just a series of directives that move a tool from place to place at a given speed. Going from Gcode to a model that you can edit is pretty difficult or impossible.
for my school project, I need to find images in a large dataset. I'm working with python and opencv. Until now, I've managed to find an exact match of an image in the dataset but it takes a lot of time even though I had 20 images for the test code. So, I've searched few pages of google and I've tried the code on these pages
image hashing
building an image hashing search engine
feature matching
Also, I've been thinking to search through the hashed dataset, save their paths, then find the best feature matching image among them. But most of the time, my narrowed down working area is so much different than what is my query image.
The image hashing is really great. It looks like what I need but there is a problem: I need to find an exact match, not similar photos. So, I'm asking you guys, if you have any suggestion or a piece of code might help or improve the reference code that I've linked, can you share it with me? I'd be really happy to try or research what you guys send or suggest.
opencv is probably the wrong tool for this. The algorithms there are geared towards finding similar matches, not exact ones. The general idea is to use machine learning to teach the code to recognize what a car looks like so it can detect cars in videos, even when the color or form changes (driving in the shadow, different make, etc).
I've found two approaches work well when trying to build an image database.
Use a normal hash algorithm like SHA-256 plus maybe some metadata (file or image size) to find matches
Resize the image down to 4x4 or even 2x2. Use the pixel RGB values as "hash".
The first approach is to reduce the image to a number. You can then put the number in a look up table. When searching for the image, apply the same hashing algorithm to the image you're looking for. Use the new number to look in the table. If it's there, you have a match.
Note: In all cases, hashing can produce the same number for different pictures. So you have to compare all the pixels of two pictures to make sure it's really an exact match. That's why it sometimes helps to add information like the picture size (in pixels, not file size in bytes).
The second approach allows to find pictures which very similar to the eye but in fact slightly different. Imagine cropping off a single pixel column on the left or tilting the image by 0.01°. To you, the image will be the same but for a computer, they will by totally different. The second approach tries to average small changes out. The cost here is that you will get more collisions, especially for B&W pictures.
Finding exact image matches using hash functions can be done with the undouble library (Disclaimer: I am also the author). It works using a multi-step process of pre-processing the images (grayscaling, normalizing, and scaling), computing the image hash, and the grouping of images based on a threshold value.
I am generating images (thumbnails) from a video every 3 seconds. Now I need to discard/remove all the similar images. Is there a way I could this?
I generate thumbnails using FFMPEG. I read about various image-diff solutions like given in this SO post, but I do not want to do this manually. How and what parameters should be considered that could tell if a particular image is similar to other images present.
You can calculate the Structural Similarity Index between images and based on the score keep or discard an image. There are other measures you can use, but basically a method that returns a score. Try PIL or OpenCV
https://pillow.readthedocs.io/en/3.1.x/reference/ImageChops.html?highlight=difference
https://www.pyimagesearch.com/2017/06/19/image-difference-with-opencv-and-python/
I dont have enough reputation to comment my idea on your problem, so i will just go ahead and post it as an answer in hope of helping you.
I am quite confused about the term "similar" but since you are reffering on video frames i am going to assume that you want to avoid having "similar" frames that have been captured because of poor camera movement. If that's the case you might want to consider using salient point descriptors.
To be more specific you can detect salient points (using for instance Harris) and then use a point descriptor algorithm (such as SURF) and discard the frames that have been found to have "too many" similar points with a pre-selected frame.
Keep in mind that in order for the above process to be successful, the frames must be as sharp as possible, i guess you don't want to extract as a thubnail a blurred frame anyway. So applying a blurred images detection might be useful in your case.
Image processing algorithms often require pixel values within a neighborhood of individual image locations. However, the neighborhood for boundary points is incomplete and needs special handling with various methods, e.g., mirror-reflecting, periodic, etc.
For linear operation, an efficient way is to turn the algorithm into a kernel and do 2D convolution. The convolution routine usual has boundary handling methods built in.
For arbitrary (non-linear) algorithms, one possibility is to pad the image first, gather all neighborhoods at once (e.g., with pad+neighbor), and do your operation in a vectorized way. However, this can be memory heavy as the doc suggested.
Loop through pixels and handle boundaries case by case is another way. But this can be slow in Python (though feasible in C/C++), and to provide all this mirror/periodic stuff seems to be cumbersome...
How to do this efficiently (and perhaps Pythonic) when implementing you own algorithms in Python?
And is there some function that returns the neighborhood of a specified pixel, and with specified boundary handling method?
The most pythonic and efficient way to do handle boundary conditions during image processing would be to use an existing library. On top of PIL and ImageMagick, I would also recommend OpenCV.
By itself, Python doesn't provide any image processing functions, so any questions about the neighborhood of a specific pixel are library-specific. Pick a library that you like and play around with it - if you can't work out how that library handles boundary conditions, come back and ask again.
PIL is one of the best known libraries to handle image processing. You might also want to take a look at ImageMagick, which i think has lots of cool image transformation features inbuilt.
I was wondering how to create a fingerprint database. If fingerprints are stored as images, how do you compare images in a database, or create an image search engine like TinEye?
I know this is a big subject, but I'm just looking for a starting point. Can this be done using Python/Django libraries and MySQL?
OpenCV comes with a sample program that does what you are looking for. It's called find_obj.py. Pull it up in your editor and change:
surf = cv2.SURF(1000)
to
surf = cv2.SURF(100)
This should find lots of "inlier" points of interest in the negative of the fingerprint scan.
You can play around with a number of the variables and eventually find the best configuration for the sort of images you're comparing. It's also fairly straightforward to alter the sample to allow you to compare a single image against an entire directory.
I should point out that this will only be effective for the sort of digitized fingerprint scans used by law enforcement.
The Python Imaging Library is probably the best library to get started on image processing with.
The library most commonly used for real time image processing (you don't need real time, but you can't go wrong with fast) is OpenCV. It has Python Bindings and built-in feature detection algorithms. See also this comparison.
For an overview of image comparison algorithms take a look at this question.
As a very simple approach you can crawl all images and compute a hash for each.
Later on, when user submits an image for a search, you compute a hash for that too and look for the same hash in your database.
However, this is really simplistic approach and will only work if searched for exact image copies. Ideally, each image should be converted to some simplified feature set (to have tolerance against different versions of the same image --- different formats, sizes, noise, etc.) used for comparison. For instance, it could be worth trying convert images (both crawled and submitted for search) to grayscale of 128x128 size and compute hash of that.