Cairo graphics: Engrave a text in a figure and also making it transparently - cairo

I have a question regarding Cairo codings (http://cairographics.org/).
I have a filled rectangle (cairo_fill (cr)), how can I cut a hole in the middle of it representing for example the letter "S"?
So basically, I want to "engrave" text in that rectangle but also making the text transparent (like cutting a hole in the rectangle).
Anyone have any tips?

Set the fill rule to even-odd and use cairo_text_path() to get the shape of the letter "S", then cairo_fill() as you do now.

Related

Cropping the minimum sized rectangle of a shape from an image

I am making a card recognition project on MATLAB and I am stuck at this point. There are images of cards and on an image I want to define the smallest rectangle that takes the card inside. Example like below
Original image
Converted image
I am currently able to convert the image to black and white (leaves me only the cards white spaces), I want to define the rectangles by the whole white spaces. E.g., if I have 3 non-lapping cards in my image, I want to have 3 images like above (doesn't matter if another cards edge appears on the image, the important part is that rectangle must pass through the edges of the selected card).
I have tried edge definition methods but wasn't successful. Thanks for your help already.
I recommend you use regionprops function from the image processing tool box, i.e.,
bb = regionprops(yourImage, 'boundingbox');
which will return the bounding box. There is a nice MATWORKS video here and you can jump to about minute 26 for what you need.

If I have 4 coordinates making a rectangle, how can I color in that area?

I have 4 coordinates assigned to variables, so I would just like to colour in the rectangle that is created from these variables. I tried the fill function, but I cant gather much information from the example.
When I try to use:
fill(bottom left point, top right point,'g')
the figure it makes only shows a line between those points, and doesn't colour in the entire area.
I'm using MATLAB R2019b, if that helps.
Also, if its possible to fill the rectangle with a pattern instead, please let me know aswell.
Thanks in advance

How do i fill this boundary of a cat with white colour using matlab?

I have already tried imfill(img) but that doesn't work at all.
I have noticed that imfill works for this purpose only for images in which the object's boundary is complete and not broken like the image i am taking .
For this particular image do the following:
Prepend a white row to the image to close the contour.
Fill the contour.
Remove the helper row.
Anything else requires more information and examples.
Matlab documentation states that "a hole is a set of background pixels that cannot be reached by filling in the background from the edge of the image". In this case any pixel can be reached from the edge because its boundary is not complete. Therefore, technically there is no hole in the image you posted.

Expand line to a polygon

I would like to expand a line to a wider polygon. Add 10 meter on both sides of the line for example.
Here is an example of what I would like
Take this line
And expand it to a wider polygon, like this
I did this manually, is there a way to do this automaticly?
Changing the KML or using a program?
Thanks
Vincent
Depending on how accurate you need this – this is not trivial.
One possible algorithm could be:
for each segment do
expand segment to rectangle with width 2r
targetShape.join(rectangle)
next
for each point do
expand point to circle with radius r
targetShape.join(circle)
next
targetShape.outerHull(precision)
Each single line in this routine is tricky and depends on your expectations.
You could leave out the circles and instead make the rectangles longer, but this wil not work on sharp turns.
All of this involves ugly calculations to figure orthogonal lines etc.
You could try it in an graphic tool, gimp or inkskape :-)

Is it possible to fill transparency with white in a texture in code?

I have some textures containing some transparency parts (a donut, for example, which would show a transparent center). What I want to do is fill the middle of the donut (or anything else) with a plain white, in code (I don't want to have a double of all my assets that need this tweak in one part of my game).
Is there a way to do this? Or do I really have to have 2 of each of my assets?
First it is possible to change a transparent texture to not-transparent, if it wasn't then graphic editors would be in trouble.
Solution 1 - Easy but takes repetitive editing by hand
The question you should be asking yourself is can you afford the transition at run time or would have two sets of textures be more efficient; from experience I find that the later tends to be more efficient.
Solution 2 - Extremely hard
You will need a shader that supports transparency and that it marks the sections that have to be shaded white. That is, it keeps track of which area will be later filled with white. It is implied that since your "donut" is already transparent on some parts then it already uses that texture that has an alpha, but you will have to write your own shader mask and be able to distinguish which is okay to fill white and which is not (fun problem here). What you need to do is find the condition in which that alpha no longer needs to be alpha and has to be white. Once the condition is met you can change the alpha of via the Color's alpha property. The only way I see you able to do this is if there is a pattern to the objects, so that you can apply some mathematical model to them and use that to find which area gets filled. If the objects are very different then the make two sets of textures starts to look more appealing.
Solution 3 - Medium with high re-use value
You could edit the textures to have two different colors, say pink and green. Green is the area that gets turned white and pink is always transparent. When green should not be white then it is transparent. You would have to edit your textures by hand as well.