What is the best way to draw 3d text with pyopengl, preferably with modern opengl ?
There seem to be quite a few examples, but mostly in old-style opengl and not for python
First you need a text outline which requires a font.
On windows you can use WinGDI fonts by creating a dummy context, selecting a created font on it and getting outlines of every character. For Truetype you load glyphs into the face and simply access their outlines.
For the front & back face of your 3D text, you need to convert the outlines/contours to triangles. Either use the good old GLU tesselator, the relative new tesselation shader of OpenGL 4 or a geometry library like CGAL. Then simply draw the triangles for the front and again with some additional depth for the back.
For the sides you simply span rectangles behind outline segments/lines with height equal to your wished text depth - these are your text sides. You can simply use a depth vector and transform everything afterwards or calculate the orthogonal vector with 2 segments.
You notice that this all can be expensive, so don't hesitate to cache as much as possible.
I referenced to C sources because i'm more familiar with these, but i bet there're python equivalents to port my explanations.
Related
I always wanted to have a device that, from a live camera feed, could detect an object, create a 3D model of it, and then identify it. It would work a lot like the Scanner tool from Subnautica. Imagine my surprise when I found OpenCV, a free-to-use computer vision tool for Python!
My first step is to get the computer to recognize that there is an object at the center of the camera feed. To do this, I found a Canny() function that could detect edges and display them as white lines in a black image, which should make a complete outline of the object in the center. I also used the floodFill() function to fill in the black zone between the white lines with gray, which would show that the computer recognizes that there is an object there. My attempt is in the following image.
The red dot is the center of the live video.
The issue is that the edge lines can have holes in them due to a blur between two colors, which can range from individual pixels to entire missing lines. As a result, the gray gets out and doesn't highlight me as the only object, and instead highlights the entire wall as well. Is there a way to fill those missing pixels in or is there a better way of doing this?
Welcome to SO and the exiting world of machine vision !
What you are describing is a very classical problem in the field, and not a trivial one at all. It depends heavily on the shape and appearance of what you define as the object of interest and the overall structure, homogeneity and color of the background. Remember, the computer has no concept of what an "object" is, the only thing it 'knows' is a matrix of numbers.
In your example, you might start out with selecting the background area by color (or hue, look up HSV). Everything else is your object. This is what classical greenscreening techniques do, and it only works with (a) a homogenous background, which does not share a color with your object and (b) a single or multiple not overlapping objects.
The problem with your edge based approach is that you won't get a closed edge safely, and deciding where the inside and outside of the object is might get tricky.
Advanced ways to do this would get you into Neural Network territory, but maybe try to get the basics down first.
Here are two links to tutorials on converting color spaces and extracting contours:
https://docs.opencv.org/4.x/df/d9d/tutorial_py_colorspaces.html
https://docs.opencv.org/3.4/d4/d73/tutorial_py_contours_begin.html
If you got that figured out, look into stereo vision or 3D imaging in general, and that subnautica scanner might just become reality some day ;)
Good luck !
I would like to get the coordinates of framed text on an image. The paragraphs have thin black borders. The rest of the image contains usual paragraphs and sketchs.
Here is an example:
Do you have any idea of what kind of algorithms should I use in Python with an image library to achieve this ? Thanks.
A few ideas to detect a framed text which largely comes down to searching boxes/rectangles of substantial size:
find contours with OpenCV, analyze shapes using cv2.approxPolyDP() polygon approximation algorithm (also known as Ramer–Douglas–Peucker algorithm). You could additionally check the aspect ratio of the bounding box to make sure the shape is a rectangle as well as check the page width as this seems to be a known metric in your case. PyImageSearch did this amazing article:
OpenCV shape detection
in a related question, there is also a suggestion to look into Hough Lines to detect a horizontal line, taking a turn a detecting vertical lines the same way. Not 100% sure how reliable this approach would be.
Once you find the box frames, the next step would be to check if there is any text inside them. Detecting text is a broader problem in general and there are many ways of doing it, here are a few examples:
apply EAST text detector
PixelLink
tesseract (e.g. via pytesseract) but not sure if this would not have too many false positives
if it is a simpler case of boxes being empty or not, you could check for average pixel values inside - e.g. with cv2.countNonZero(). Examples:
How to identify empty rectangle using OpenCV
Count the black pixels using OpenCV
Additional references:
ideas on quadrangle/rectangle detection using convolutional neural networks
I would like to extract the bounding box of a SVG drawing.
Since Python is already available on the system and also used to perform other tasks, I don't want to use JavaScript or any other language. My understanding is if the bounding box of a single element can be calculated (but I don't know how).
The bounding box of the whole drawing is just the minimum and maximum x,y values a of all elements, hence probably the bounding boxes of all elements need to be calculated.
I am a Python beginner, but svgwrite is probably not the right module and so far I was scared by the installation of rsvg on a Windows system.
Thank you for any hints and pointing me to the right direction.
I need to detect the different shapes on the image such as ellipses and circles with a dashed-border.
Using OpenCV causes some troubles, for e.g. openCV detects not a contour but an amount of small separated lines(contours).
In case of ellipse with a "dashed-border" I need it to be as single contour, not a separated objects.
What would you suggest to solve this problem?
I also need to recognize the ellipses(or circles) with solid border and ellipses(circles) with a dashed-border
Ellipse with solid border
Ellipse with dashed border:
The Hough transform should not have any troubles with either dashed or solid borders, or a mix thereof. I have used it to detect dashed lines in one of my project and ellipses should not be much harder.
OpenCV by default only implements the Hough transform for circles and straight lines but there is a blog entry with code on how to adapt it for elliptical shapes.
The better idea there is to use an specific transform to detect circle and ellipses rather than a general "find contour" call.
I am not enough acquainted with OpenCV to know if there this sort of detection is built in there - it should be.
An internet search for the more generic "image ellipse detection" shows it is not a settled matter - unlike square and rectangle detection, so whatever OpenCV already contains will have to be tunned up.
(The basic idea is that the underlying algorithm creates a new n-dimensional space where every possible paramterized ellipse for the target image could be represented (with, for example, an axis for each x and y center coordinates, w and h radii, and one for rotation), and fill in hits in this matrix according to the pixels contrast in the target image).- Sebastian's answer put the correct name for this approach I could not recall: "Hough transform", and provides the needed links on how it works, and how to extend it on OpenCV.
I have an arbitrary rectangular Cartesian grid divided into potentially 10^6 or so rectangular cells. (Arbitrary means that the $x$ grid is along points $x_1,...x_n$ and the same goes for the $y$ grid.) I would like to draw an arbitrary object on top of it (say a rotated rectangle, or a circle), and efficiently calculate what fraction of each cell is overlapped by the object: if the cell is entirely inside the bounds of the object, 1.0; if the cell is entirely outside, 0.0; if half of the cell is covered by the object, 0.5. If you displayed this as an image and scaled it where 1 is black and 0 is white, the result would look like an antialiased drawing of the black object.
My application for this question is in Python, and it seems like this capability might be provided by some existing graphics library. Is there a Python module that will test for the fractional intersection of a rectangle and an arbitrary object? Is there a Python library that can at least efficiently test if a point is inside an arbitrary object like a rotated rectangle?
You could use PyCairo, which has fast native routines to do its drawing. It's antialiased by default.
Implementing the drawing algorithms in Python would be very slow.
To find the area of a trapezoid resulting from a polygon-square intersection, you can follow the process described by Sean Barrett at https://nothings.org/gamedev/rasterize/
The shapely Python library can find the area of a trapezoid and perform point-in-object tests. However, for best performance this sounds like something that you'd want to write in C/C++ and provide numpy bindings.