image-processing,glsl,photoshop , OpenGL GLSL: How to implement the concept of gradient map in photoshop using fragment shader?

OpenGL GLSL: How to implement the concept of gradient map in photoshop using fragment shader?


Tag: image-processing,glsl,photoshop

This may be not the perfect question for stackoverflow but I have asked this in graphicdesign network of stackexchange but did not get any answer so posting it here. I am trying to implement gradient map of Photoshop programmatically using glsl fragment shader. Consider below shown gradient map where all white components are replaced by green and black by blue. For other color components Photoshop calculates based on the linear gradient between blue and green. I know there is a math behind this calculation. Does any one know formula to find output color for particular input color value?

enter image description here


Based on the image and description, it looks like this maps the original grayscale value of the image to the blue-green gradient.

There are various ways of converting color to grayscale, depending on the color system used. Many of the simple one are just a weighted sum of the RGB components. For example a widely used formula for converting RGB to brightness (Y) is:

Y = 0.299 * R + 0.587 * G + 0.114 * B

See the Grayscale wikipedia page for more background on the topic and all the different options.

Then you can use a linear interpolation of blue and green with the grayscale value as the parameter. The math for this would be easy anyway, but GLSL even has a built-in mix() function for this purpose.

Some possible code fragments for GLSL, based on reading the original color from a texture Tex using texture coordinates TexCoord:

uniform sampler2D Tex;
in vec2 TexCoord;
out vec4 FragColor;
    vec4 origColor = texture(Tex, TexCoord);
    float grayscaleValue = dot(origColor.rgb, vec3(0.299, 0.587, 0.114));
    FragColor = mix(vec4(0.0, 0.0, 1.0, 1.0), vec4(0.0, 1.0, 0.0, 1.0), grayscaleValue);

In this code, vec3(0.299, 0.587, 0.114) contains the coefficients from the RGB to grayscale (brightness) formula above. The first two arguments of the mix() function are the start and end colors of the gradient:

Note that a simple linear interpolation between the two colors in RGB space is not ideal, particularly if the colors are very different. You can make this more elaborate, and potentially get better quality, by operating in a different color space like HLS.

Another option to possibly improve the visual appearance is that you use more than two colors to specify the gradient. For example, you could assign blue to brightness 0.0, cyan to brightness 0.5, and green to brightness 1.0. It requires a little more logic than the GLSL code above, but it's just an extension of the same principle.


thicken an object of image to a curve in matlab

I have a labeled matrix containing two objects. How can I thicken an object to a curve? Actually I have the following image: and I want this: Each pixel of the resulting curve is the median of each column. But if you have another idea, it is acceptable, because I...

Segmentation image processing using MATLAB

I am trying to segment my image using the following code. image=imread('mob.jpg'); image = im2bw(image); L = bwlabel(image,8);% Calculating connected components mx=max(max(L)) [r,c] = find(L==1); rc = [r c]; [sx sy]=size(rc); n1=zeros(imx,imy); for i=1:sx x1=rc(i,1); y1=rc(i,2); n1(x1,y1)=255; end imshow(image); figure, imshow(n1); This was my input image- I wanted to separate...

MATLAB ConnectedComponentLabeler does not work in for loop

I am trying to get a set of binary images' eccentricity and solidity values using the regionprops function. I obtain the label matrix using the vision.ConnectedComponentLabeler function. This is the code I have so far: files = getFiles('images'); ecc = zeros(length(files)); %eccentricity values sol = zeros(length(files)); %solidity values ccl =...

How to separate the patches in the image?

I want to initialize the dictionary of this image. The image is the same as this one: image source The size of the grey image is 320*512 double. I want to separate the 40 brain image patches in this grey image. I have two questions 1) how to get the...

How to call OpenCV's MatchTemplate method from C#

I'm trying to get the template's position on the image using OpenCVSharp library from NuGet. Here is the code I've wrote: var image = Cv.LoadImage("Image.png"); var template = Cv.LoadImage("Template.png"); var w = (image.Width - template.Width) + 1; var h = (image.Height - template.Height) + 1; IplImage result = new IplImage(w,...

How can I determine the length of a multi-page TIFF using Python Image Library (PIL)?

I know that the and Image.tell() methods of PIL allow me to go to a particular frame, and list the current frame, respectively. I would like to know how many frames there are total. Is there a function for getting this info? Alternatively, is there a way in python...

Zero out portion of multidim numpy array

I have an numpy array with dimensions (200, 200, 3). It is an RGB image. I also have the (xmin,ymin,xmax,ymax) coordinates of a region of this image that I would like to set to zero. This region should be zero in all three channels. I can of course solve this...

Using key positions in steganography using DCT

I have a question about steganography. Once the DCT matrix's are computed and the LSB's are replaced can we assume that the location at which the data is entered in the DCT of the cover image is know at the receiver? If not then how is the image reconstructed?

How can I replace the white rectangle within an image using ImageMagick?

Overview: The first picture is my original image. Here I want to replace the white rectangle shown with another image. My approach: I have created a mask image using floodfill and it looks as: Problem: Now I would like to get the distance or co-ordinates of the rectangle in the...

how to detect partial corrupted images in Jpg/Jpeg format

In a very big image dataset, we have some corrupted images as following images. These images can be viewed without any problem, but human eyes can see some corrupted areas in gray color. How can I detect these corrupted images? Actually I have written a script in Matlab for detecting....

OpenGL GLSL: How to implement the concept of gradient map in photoshop using fragment shader?

This may be not the perfect question for stackoverflow but I have asked this in graphicdesign network of stackexchange but did not get any answer so posting it here. I am trying to implement gradient map of Photoshop programmatically using glsl fragment shader. Consider below shown gradient map where all...

Overlaying a transparent color over a Texture with GLSL

I have an image that I am loading using the Slick library, and the image renders fine without my shader active. When I use my shader to overlay a transparent color over the image the entire image is replaced by the transparent color. without the shader With the shader Vertex...

Convert Android image into a file that MATLAB can read

I´m working on a project related with image recognition using MATLAB and I'm currently using an Android app to help with some pre-processing steps. I thought it was going to be easy to work with matrices instead of bitmaps. I finally managed to finish my algorithm and to import it...

Increase Size of Black Bullets in Image in C#

Given the following image: I need to output this image: I want to increase the size of Black Pixels in the image using C# or AForge.Net...

adaptive thresholding ---ValueError: too many values to unpack

I'm pretty amateur at image processing. I could successfully do normal thresholding but however I'm facing an error in Adaptive Thresholding. Here is my code: import cv2 import numpy as np img = cv2.imread("vehicle004.jpg") img = cv2.medianBlur(img,5) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) _,th2=cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2) cv2.imshow("window2",th2) cv2.waitKey(0) cv2.destroyAllWindows() Error Message: line 7, in <module>...

How to identify Bimodal Histograms automatically?

I have a problem where i receive a lot of histograms from some database images. Those histograms are represented as vectors(0...256), and i have to identify and work with the bimodal histograms. Well, is there a formula to automatically identify wich histograms are bimodal and wich arent? Since they are...

Error in writing a grayscale image

In the following code am trying to read a grayscale image, store the pixel values in a 2D array and rewrite the image with a different name. The code is package dct; import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.awt.image.Raster; import; import javax.imageio.ImageIO; public class writeGrayScale { public static void main(String[]...

How to create thumbnails using opencv-python?

I'm trying to downsample my image(using anti-aliasing) using Python-Pillow's im.thumbnail() method. My code looks like: MAXSIZE = 1024 im.thumbnail(MAXSIZE, Image.ANTIALIAS) Can you tell me some alternative in opencv-python to perform this re-sizing operation ?...

Display .bin depth image in Matlab

Here is the .bin image which I saved in c#. Please anyone help to view this in MATLAB. the dimension of image 424 x 512. I have tried this code, but its not working correctly file = fopen('test0.bin', 'r'); A = fread(file, 424*512, 'uint16=>uint16'); A1 = reshape(A, 424, 512); imagesc(A1)...

How to concatenate 3 histograms on the same graph in Matlab

I have calculated 3 histograms for an rgb image using imhist function in Matlab, one for each channel. I want to plot these histograms on the same graph, where for instance, the histogram for the first (red) channel stretches on the x axis from 0 to 255, the histogram for...

Lab-image values on both of a and b axises exceeds the range

i converted a color image to Lab-color space, and when i displayed the values of the Lab-image, i found that the values of the pixels on the a-axis and b-axis are greater than 128!! while the values on the a-axis and b-axis should range from -128 to +128 please let...

Warning: IMSHOW(I,N) is an obsolete syntax. Your grayscale image will be displayed using 256 shades of gray

I get this warning:Warning: IMSHOW(I,N) is an obsolete syntax. Your grayscale image will be displayed using 256 shades of gray..please help me. Ihave implemented 2d dct.why its not working for m=64; m=input('enter the basis matrix dimension:'); n=m; alpha2=ones(1,n)*sqrt(2/n); alpha2(1)=sqrt(1/n); alpha1=ones(1,m)*sqrt(2/m); alpha1(1)=sqrt(1/m); for u=0:m-1 for v=0:n-1 for x=0:m-1 for y=0:n-1 a(u+1,v+1,x+1,y+1)=alpha1(u+1)*alpha2(v+1)*cos((2*x+1)*u*pi/(2*n))*cos((2*y+1)*v*pi/(2*n));...

Create mask from bwtraceboundary in Matlab

I'm trying to create a mask (or similar result) in order to erase pieces of a binary image that are not attached to the object surrounded by the boundary. I saw this thread ( to do this from bwboundaries, but I'm having trouble making suitable changes to it. My goal...

How to prevent exceeding matrix dimensions while dividing an image into blocks?

I have an image which I want to divide into overlapping blocks. I have set the box to be of size 8 rows and 8 columns, and the overlapping factor to be 4 rows/columns. This is what I have written to solve this: img = imread('a03-017-05.png'); overlap = 4 count...

how can i know if the image is in RGB or BGR format?

is there any way to know in advance if an image used as an input to a system is in RGB or BGR format? I am using opencv with java API and i would like to convert an input image into grayscale or Lab* color space, and in opencv you...

conversion between Mat and Mat1b/Mat3b

I want to match my code into a given interface. Inside my class OperateImage in all methods I use cv::Mat format. When putting it in SubMain function which uses cv::Mat3b and returns cv::Mat1b it does not work. How can I change it so that I can use my written class?...

Java encode raw bytes into image simple image fomat/ file

I am very new to image encoding and would rather not learn a whole lot about it. Basically I'm taking greyscale byte array where each byte equals one pixel. I'm getting this data from mnist where I get 28x28 byte images. Anyway, bellow is my code, so you understand what...

OpenGL: Defining variables in shaders

My OpenGL program, using GLSL for shaders, has a simple vertex and fragment shader (given by a tutorial). The vertex shader is: #version 330 layout (location = 0) in vec3 Position; void main() { gl_Position = vec4(0.5 * Position.x, 0.5 * Position.y, Position.z, 1.0); } And the fragment shader is:...

Pass Buffer to ChildProcess Node.js

Here I have on Node.Js where I want to do Image Processing in a Sub Process. As you will see I take the file image.jpg and want to write it back to hello.jpg in a subprocess: var node = require('child_process').spawn('node',['-i']); var fs = require('fs'); node.stdout.on('data',function(data) { var fs = require('fs');...

OpenCV get coordinates of red-only rectangle area

I have the following output from red-only filtration done by the following algorithm: cv::Mat findColor(const cv::Mat & inputBGRimage, int rng=20) { // Make sure that your input image uses the channel order B, G, R (check not implemented). cv::Mat mt1, mt2; cv::Mat input = inputBGRimage.clone(); cv::Mat imageHSV; //(input.rows, input.cols, CV_8UC3);...

Displaying surface with non-rectangular boundary

Refer to the attached image. I want to display the image in Matlab using the function surf(). However, I only want to display the region of the actual object without the background (the pale-green region surrounding the actual object which has 0 value). How to do that? I tried replacing...

How can I render an infinite 2D grid in GLSL?

Ideally, what I'd like to do is draw a single quad and have GLSL handle the creation of the actual gridlines. In my attempt so far the vertex shader: #version 400 layout (location = 0) in vec4 in_position; layout (location = 2) in vec3 in_UV; uniform mat4 model; uniform mat4...

Ambient and Specular lighting not working correctly in GLSL

In my lighting scene, for some reason the ambient lighting isn't working at all. The whole model is the same brightness, no matter which way it is facing. I tried getting rid of the attenuation but it still has the same results. Along with that, the specular lighting is always...

GLSL - program link error: Slot 0 unavailable from layout location request

I'm trying to draw a textured quad copying some code from a tutorial but I'm afraid there is a problem with the shaders. Both the vertex shader and the fragment shader compilation works, but when linking the program I get the error: ERROR: Active attribute aliasing. Slot 0 unavailable for...

Getting an image to render with php

To keep it simple, I'm trying to display an image using php to help try and prevent any kind of a security loophole. I have the image being read through a php script and I am wanting to render it, so instead of going to I want to have...

Saving images with more than 8 bits per pixel in matlab

I need to save a set of pre-processing images in matlab, resulting in grayscale images. The problem is the fact that these pre-processed images have pixel values higher than 255. If I save them with imwrite() as, for instance, .PNG files, does matlab normalize the values to be in [0,255]...

How to create latitudinal (horizontal) contour lines in GLSL?

I'm aiming for this effect: (horizontal-only contour lines): I did find this example, however it creates horizontal and vertical contour lines. I can't quite wrap my head around how the call to fwidth() is generating the lines. uniform float gsize;//size of the grid uniform float gwidth;//grid lines'width in pixels varying...

Captcha with merging a number with real image

I have searched for captcha generation tools, but I could not find any tool which can generate the below captcha pictures. You can seen these when you want to register on google. It uses a number merged on a real picture and produce a new real image picture. Do you...

imread function doesn't work in matlab

I am new to Image processing and learning matlab. Actually till now I have used matlab cloud version where uploading an image was directly possible and didn't face any issue. But now I am facing an extreme problem of uploading an image in matlab offline version software. I imported an...

Connecting two binary objects in matlab

I have a binary matrix containing several binary objects and I want to bridge between them. Actually I have the following picture: And the result has to be like this: Is there any function or a shortcut way, other than loops, for this problem?...

Modern GLSL ( opengl 3+ ) : Implementing phong effect correctly;

I am implementing a basic phong lighting GLSL shader; I have looked up some things on the internet, and found that the phong effect was created by adding an ambient, diffuse, and specular layer on the object (see image below, from tom dalling's site); problem is I have seen a...

Using OpenCV in Swift iOS

After adding the OpenCV 2 framework in my xcode project, I tried searching for samlpes or tutorials for integration with swift. Are there any good tutorials for the same?...

OpenCV / Image Processing techniques to find the centers of bright spots in an image

I'm currently doing a project based on the methodology described in this paper: Camera calibration from a single night sky image As a beginner in computer vision, I do not quite understand how I can implement the method used in the paper to find the centre of all the bright...

Reading all the files in sequence in MATLAB

I am trying to read all the images in the folder in MATLAB using this code flst=dir(str_Expfold); But it shows me output like this. which is not the sequence as i want. Can anyone please tell me how can i read all of them in sequence? for giving downmark, please...

Displaying a 32-bit image with NaN values (ImageJ)

I wrote a multilanguage 3-D image denoising ImageJ plugin that does some operations on an image and returns the denoised image as a 1-D array. The 1-D array contains NaN values (around the edges). The 1-D array is converted back into an image stack and displayed. It is simply black....

How to convert byte array to image in php?

I want to convert the byte array from a webservice to image. The webservice response array looks like the following but I could not figure out how to render this array back to an image. Please help me 'MemberImage' => array (size=151745) 0 => int 255 1 => int 216...

Extracting polygon given coordinates from an image using OpenCV

I've a set of points like the following: <data:polygon> <data:point x="542" y="107"/> <data:point x="562" y="102"/> <data:point x="582" y="110"/> <data:point x="598" y="142"/> <data:point x="600" y="192"/> <data:point x="601" y="225"/> <data:point x="592" y="261"/> <data:point x="572" y="263"/> <data:point x="551" y="245"/> <data:point x="526" y="220"/> <data:point x="520" y="188"/> <data:point x="518" y="152"/> <data:point x="525" y="127"/> <data:point...

Bandpass Filter in Python for Image Processing

I have a noisy dataset (a stack of images) which films dim particles moving about some really bright artefacts (which are immobilized). I would like to somehow remove the immobilized artefacts from the images by applying some sort of bandpass filter wherein only pixels within a specific range are converted...

Detecting individual images in an array of images

I'm building a photographic film scanner. The electronic hardware is done now I have to finish the mechanical advance mechanism then I'm almost done. I'm using a line scan sensor so it's one pixel width by 2000 height. The data stream I will be sending to the PC over USB...