Metal - How to overlap textures based on color - ios

I'm trying to use a render pass descriptor to draw two grayscale textures. I am drawing a black square first, then a light gray square after. The second square partially covers the first.
With this setup, the light gray square will always appear in front of the black square because it was drawn most recently in the render pass. However, I would like to know if there is a way to draw the black square above the light gray one based on its brightness. Since the squares only partially overlap is there a way to still have the black square appear on top simply because it has a darker pixel value?
Currently it looks something like this, where the gray square is drawn second so it appears on top.
What I would like is to be able to still draw the gray square second, but have it appear underneath based on the pixel brightness, like so:

I think MTLBlendOperationMin will do what you want: https://developer.apple.com/documentation/metal/mtlblendoperation/mtlblendoperationmin?language=objc

Related

Draw a border around the bright part of the image

I have an image with a bright center but dark edges. I need to draw rectangle at a certain brightness, so that the darker areas remain outside. Or get the coordinates of this frame. Preferably using OpenCV, the programming language is not important.

Detecting "surrounded" areas in a binary image

from performing another operation, I get an B/W ( binary ) image which has white and black areas. Now I want to find and floodfill the blacj areas that are completely surrounded by white and not touching the Image border.
The "brute-force" approach i used, which is basically iterating over all pixels( all but the "border" rows/cols), if it finds a black one, I look at the neighbours ( mark them as "visited" ) and if they are black recursively go to their neighbours. And if I only hit white pixels and don't end up at a border I floodfill the area.
This can take a while on a high resolution image.
Is there a not too complicated faster way to to this?
Thank you.
As you have a binary image, you can perform a connected component labeling of the black components. All the components found are surrounded with white. Then you go along the borders in order to find the components that touch the border, and you delete them.
An other simpler and faster solution, would be to go along the borders, and as soon as you find a black pixel, you set a seed that expands until all the black pixels that touch the original pixel are white. Doing that, you delete all the black pixels touching the borders. It will remain only the black components that do not touch the borders.
If most black areas are not touching the border, doing the reverse is likely faster (but equally complicated).
From the border mark any reachable pixel (reachable meaning you can get to the border via only black pixels). After this do a pass of the whole image. Anything black and not visited will be a surrounded area.

Line detection against darker shades

I wants to detect lines from an image with black line drawing over white papers.
It could be easy if its ideal 'black and white', using histogram threshold would do.
But, as the image attached shows, some lines (e.g. in the light red circle) are in gray lighter than the shades (e.g. in the dark red circle). So some shades are obtained before light lines using histogram threshold.
Is there any ideas to divide lines from shades with some 'knowledge'? Thanks!
Edit:
Here are the raw images, a bit small because they are of original resolution.
Thanks :-)
I would add another method using Gaussian blur other than erosion+dilation, for your reference:
file='http://i.stack.imgur.com/oEjtT.png';
I=imread(file);
h = fspecial('gaussian',5,4);
I1=imfilter(I,h,'replicate');
h = fspecial('gaussian',5);
I2=imfilter(I,h,'replicate');
I3=I1-I2;
I3=double(I3(:,:,1));
I3=I3.*(I3>5);
imshow(I3)

Resize png image in Delphi - incorrect alpha channel

I am resizing png images which might have alpha channel.
Everything works good, with one exception:
I get some gray pixels around the transparent areas.
The original image doesn't have any drop shadows.
Is there a way to fix this / work it around?
I am using SmoothResize by Gustavo Daud (See the first answer to this question), to resize the png image.
I cannot provide the code that I am using as I did not write it and do not have the author's permission to publish it.
I suspect that is caused by 2 things: funny RGBA values in PNG and naive resizing code.
You need to check your PNG contents. You are looking for RGB values in transparent areas. Despite transparent areas having Alpha at 0, they still have an RGB info. In your case I would expect that transparent areas are filled with black RGB value. That is what might cause the grey outline after resize if the resize is done naively. Example: What happens if code resizes 2 adjustent pixels (0,0,0,0) and (255,255,255,255) into one? Both pixels contribute by 50% The result is 128,128,128,128) which is semi-transparent grey. Same thing happens when you upscale by e.g x1.5, the added pixel inbetween original two will be grey. Usually that does not happen because image-editing software is smart enough to fill those invisible pixels with color from nearest visible pixel.
You can try to "fix" the PNG by filling transparent areas with white (or another color that is on border of your images).
Another approach is to use an advanced resizing code (write or find library), that will ignore transparent pixels RGB values (e.g. by taking RGB from closest non-transparent pixel).

Intersection Region Color

i have drawn two semitransparent circles which intersect each other. I have found that the intersection region is deeper in color than other regions. is there any way to make the whole shape as one semitransparent color (color shouldn't be deeper in one area than others)?
is it possible to send me any sample code to solve the problem?
right now in the draw method, i am using the following code:
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(textureCircle1, spritePositionCircle1, new Color(255, 255, 255, (int)(150)));
spriteBatch.Draw(textureCircle2, spritePositionCircle2, new Color(255, 255, 255, (int)(150)));
spriteBatch.End();
base.Draw(gameTime);
I'm not an XNA guy, so you might have to do some translation.
Could you perhaps render them off screen on as white on black monochrome, and take the resulting image, make the white the transparent color you want and the black completely transparent?
I'm not sure how you'd code that up, but that's the approach I'd research.
Check the alpha values of the pixels which you think are less transparent (you're drawing these onto their own surface, not directly to the backbuffer, right?). They may just seem less transparent because their combined color is darker.
If they truly are less transparent, change the transparency of every pixel on the surface to the same value (I'm afraid I don't know how to do this in XNA).
If they only seem less transparent, try drawing your sprites onto the surface completely opaquely (so one will completely overwrite the other), then again change the transparency of the surface as a whole.

Resources