How to set custom(some calculate logic) color according to each pixel value(R,G,B) using KONVA - html5-canvas

I am trying to convert the color of the pixel using KONVA image class.
The conversion method is as follows.
var avg = (R + G + B)/3;
if(avg > 200){
pixelColorR = 255;
pixelColorG = 255;
pixelColorB = 255;
}else {
pixelColorR = 200;
pixelColorG = 200;
pixelColorB = 200;
}
So I'd like to get two colored grayscale images from RGB images like the following.
===>>>
But I can't figure out how to get the color of each pixel using Konva class.
And how to set custom colors.
Anyone can help me?
Thank you.

Related

Add overlay image on top of other image

Suppose that I have an RGB image RGB and a binary image binary that contains the result of segmentation of image RGB. How to draw the image binary on top of image 'RGB` and show the segmentation as a red transparent area? I tried the following but I got an error. Please help me to figure out the best way in MATLAB
I=imread('RGB.png');
[M,N,C] = size(I);
h=imshow(I);
alpha= imread('binary.png');
alpha = cat(3,alpha,zeros(M,N), zeros(M,N));
set(h, 'AlphaData', alpha);
Here are the input images:
Here is a method to add red overlays in select regions of the mask. Modifications to these scripts can be made to fill in the remaining regions with the colours white or black. The regions of interest on the mask are selected using a logical array.
Overlay Red Over White Region of Mask
Overlay_Opacity = 0.5;
Image =imread('RGB.png');
imshow(Image);
Red_Channel = imread('binary.png');
White_Mask_Region = Overlay_Opacity*(Red_Channel ~= 0);
Overlay_Image(:,:,1) = White_Mask_Region;
Overlay_Image(:,:,2) = 0;
Overlay_Image(:,:,3) = 0;
hold on
Overlay = image(Overlay_Image);
Overlay.AlphaData = White_Mask_Region;
saveas(gcf,'Overlay_1.png');
Overlay Red Over Black Region of Mask
Overlay_Opacity = 0.5;
Image =imread('RGB.png');
imshow(Image);
Red_Channel = imread('binary.png');
Black_Mask_Region = Overlay_Opacity*(Red_Channel == 0);
Overlay_Image(:,:,1) = Black_Mask_Region;
Overlay_Image(:,:,2) = 0;
Overlay_Image(:,:,3) = 0;
hold on
Overlay = image(Overlay_Image);
Overlay.AlphaData = Black_Mask_Region;
saveas(gcf,'Overlay_2.png');
You simply use your binary alpha (one layer only) as AlphaData.
If you have the Image Processing Toolbox, this function will do what you want:
https://www.mathworks.com/help/images/ref/labeloverlay.html

How to disable Center Color in polygon with multicolored gradient?

I am trying to build a polygon using only two colors for all vertex. But the gdiplus library automatically inserts an white center color blending all the figure. I would like to disable the center color, instead workarounding it by using SetCenterColor() available in PathGradientBrush class. Shifting the default position using SetCenterPoint() to a far way position is very inelegant. Is that possible?
Thanks
A sample follows:
CMyGDIPlus gdi(this); // use your class instead
using namespace Gdiplus;
Graphics & graphics = gdi.GetGraphics();
graphics.SetSmoothingMode(SmoothingModeNone);
Gdiplus::Rect gRect;
graphics.GetVisibleClipBounds(&gRect);
int i;
int colorSize = 4;
GraphicsPath path;
Point arrPoint[4];
Color arrColors[4];
arrPoint[0].X = gRect.GetLeft();
arrPoint[0].Y = gRect.GetTop();
arrPoint[1].X = gRect.GetRight();
arrPoint[1].Y = gRect.GetTop()+100;
arrPoint[2].X = gRect.GetRight();
arrPoint[2].Y = gRect.GetBottom();
arrPoint[3].X = gRect.GetLeft();
arrPoint[3].Y = gRect.GetBottom()-100;
for(i = 0; i < colorSize; i++)
{
if(i < 2)
arrColors[i].SetFromCOLORREF(RGB(0, 128, 0)); // green
else
arrColors[i].SetFromCOLORREF(RGB(0, 0, 192)); // blue
}
path.AddLines(arrPoint, 4);
PathGradientBrush pathBrush(&path);
pathBrush.SetSurroundColors(arrColors, &colorSize);
pathBrush.SetGammaCorrection(TRUE);
graphics.FillPath(&pathBrush, &path);
You only need to calculate the color value of the center point.
To average the values (e.g. (r1 + r2) / 2). This works better for lightening/darkening colors and creating gradients.
Refer: Algorithm for Additive Color Mixing for RGB Values
Add : pathBrush.SetCenterColor(Color(0, 128*0.5, 192*0.5));
Debug:

How to extract color shade from a given sample image to convert another image using color of sample image?

I have a sample image and a target image. I want to transfer the color shades of sample image to target image. Please tell me how to extract the color from sample image.
Here the images:
input source image:
input map for desired output image
output image
You can use a technique called "Histogram matching" (another description)
Basically, you use the histogram for your source image as a goal and transform the values for each input map pixel to get the output histogram as close to source as possible. You do it for each rgb channel of the image.
Here is my python code for that:
from scipy.misc import imsave, imread
import numpy as np
imsrc = imread("source.jpg")
imtint = imread("tint_target.jpg")
nbr_bins=255
imres = imsrc.copy()
for d in range(3):
imhist,bins = np.histogram(imsrc[:,:,d].flatten(),nbr_bins,normed=True)
tinthist,bins = np.histogram(imtint[:,:,d].flatten(),nbr_bins,normed=True)
cdfsrc = imhist.cumsum() #cumulative distribution function
cdfsrc = (255 * cdfsrc / cdfsrc[-1]).astype(np.uint8) #normalize
cdftint = tinthist.cumsum() #cumulative distribution function
cdftint = (255 * cdftint / cdftint[-1]).astype(np.uint8) #normalize
im2 = np.interp(imsrc[:,:,d].flatten(),bins[:-1],cdfsrc)
im3 = np.interp(imsrc[:,:,d].flatten(),cdftint, bins[:-1])
imres[:,:,d] = im3.reshape((imsrc.shape[0],imsrc.shape[1] ))
imsave("histnormresult.jpg", imres)
The output for you samples will look like that:
You could also try making the same in HSV colorspace - it might give better results.
I think the hardest part is to determine the dominant color of the first image. Just looking at it, with all the highlights and shadows, the best overall color will be the one that has the highest combination of brightness and saturation. I start with a blurred image to reduce the effects of noise and other anomalies, then convert each pixel to the HSV color space for the brightness and saturation measurement. Here's how it looks in Python with PIL and colorsys:
blurred = im1.filter(ImageFilter.BLUR)
ld = blurred.load()
max_hsv = (0, 0, 0)
for y in range(blurred.size[1]):
for x in range(blurred.size[0]):
r, g, b = tuple(c / 255. for c in ld[x, y])
h, s, v = colorsys.rgb_to_hsv(r, g, b)
if s + v > max_hsv[1] + max_hsv[2]:
max_hsv = h, s, v
r, g, b = tuple(int(c * 255) for c in colorsys.hsv_to_rgb(*max_hsv))
For your image I get a color of (210, 61, 74) which looks like:
From that point it's just a matter of transferring the hue and saturation to the other image.
The histogram matching solutions above did not work for me. Here is my own, based on OpenCV:
def match_image_histograms(image, reference):
chans1 = cv2.split(image)
chans2 = cv2.split(reference)
new_chans = []
for ch1, ch2 in zip(chans1, chans2):
hist1 = cv2.calcHist([ch1], [0], None, [256], [0, 256])
hist1 /= hist1.sum()
hist2 = cv2.calcHist([ch2], [0], None, [256], [0, 256])
hist2 /= hist2.sum()
lut = np.searchsorted(hist1.cumsum(), hist2.cumsum())
new_chans.append(cv2.LUT(ch1, lut))
return cv2.merge(new_chans).astype('uint8')
obtain average color from color map
ignore saturated white/black colors
convert light map to grayscale
change dynamic range of lightmap to match your desired output
I use max dynamic range. You could compute the range of color map and set it for light map
multiply the light map by avg color
This is how it looks like:
And this is the C++ source code
//picture pic0,pic1,pic2;
// pic0 - source color
// pic1 - source light map
// pic2 - output
int x,y,rr,gg,bb,i,i0,i1;
double r,g,b,a;
// init output as source light map in grayscale i=r+g+b
pic2=pic1;
pic2.rgb2i();
// change light map dynamic range to maximum
i0=pic2.p[0][0].dd; // min
i1=pic2.p[0][0].dd; // max
for (y=0;y<pic2.ys;y++)
for (x=0;x<pic2.xs;x++)
{
i=pic2.p[y][x].dd;
if (i0>i) i0=i;
if (i1<i) i1=i;
}
for (y=0;y<pic2.ys;y++)
for (x=0;x<pic2.xs;x++)
{
i=pic2.p[y][x].dd;
i=(i-i0)*767/(i1-i0);
pic2.p[y][x].dd=i;
}
// extract average color from color map (normalized to unit vecotr)
for (r=0.0,g=0.0,b=0.0,y=0;y<pic0.ys;y++)
for (x=0;x<pic0.xs;x++)
{
rr=BYTE(pic0.p[y][x].db[picture::_r]);
gg=BYTE(pic0.p[y][x].db[picture::_g]);
bb=BYTE(pic0.p[y][x].db[picture::_b]);
i=rr+gg+bb;
if (i<400) // ignore saturated colors (whiteish) 3*255=white
if (i>16) // ignore too dark colors (whiteish) 0=black
{
r+=rr;
g+=gg;
b+=bb;
}
}
a=1.0/sqrt((r*r)+(g*g)+(b*b)); r*=a; g*=a; b*=a;
// recolor output
for (y=0;y<pic2.ys;y++)
for (x=0;x<pic2.xs;x++)
{
a=DWORD(pic2.p[y][x].dd);
rr=r*a; if (rr>255) rr=255; pic2.p[y][x].db[picture::_r]=BYTE(rr);
gg=g*a; if (gg>255) gg=255; pic2.p[y][x].db[picture::_g]=BYTE(gg);
bb=b*a; if (bb>255) bb=255; pic2.p[y][x].db[picture::_b]=BYTE(bb);
}
I am using own picture class so here some members:
xs,ys size of image in pixels
p[y][x].dd is pixel at (x,y) position as 32 bit integer type
p[y][x].db[4] is pixel access by color bands (r,g,b,a)
[notes]
If this does not meet your needs then please specify more and add more images. Because your current example is really not self explanatonary
Regarding previous answer, one thing to be careful with:
once the CDF will reach its maximum (=1), the interpolation will get mislead and will match wrongly your values. To avoid this, you should provide the interpolation function only the part of CDF meaningful (not after where it reaches 1) and the corresponding bins. Here the answer adapted:
from scipy.misc import imsave, imread
import numpy as np
imsrc = imread("source.jpg")
imtint = imread("tint_target.jpg")
nbr_bins=255
imres = imsrc.copy()
for d in range(3):
imhist,bins = np.histogram(imsrc[:,:,d].flatten(),nbr_bins,normed=True)
tinthist,bins = np.histogram(imtint[:,:,d].flatten(),nbr_bins,normed=True)
cdfsrc = imhist.cumsum() #cumulative distribution function
cdfsrc = (255 * cdfsrc / cdfsrc[-1]).astype(np.uint8) #normalize
cdftint = tinthist.cumsum() #cumulative distribution function
cdftint = (255 * cdftint / cdftint[-1]).astype(np.uint8) #normalize
im2 = np.interp(imsrc[:,:,d].flatten(),bins[:-1],cdfsrc)
if (cdftint==1).sum()>0:
idx_max = np.where(cdftint==1)[0][0]
im3 = np.interp(im2,cdftint[:idx_max+1], bins[:idx_max+1])
else:
im3 = np.interp(im2,cdftint, bins[:-1])
Enjoy!

Adaptive Contrast Enhancement(ACE)

How can i separate the image as a three region(dark, mid, bright)?any matlab commands are available for that?
The image processing toolbox has some options for segmenting images based on color. More information here. If you only need to select pixels based on intensity you can use Boolean operators. Here's an example of that approach:
% create an example image with three regions of intensity
im = ones(100,100);
im(1:25, 1:30) = 256;
im(75:end, 7:end) = 128;
% add some random noise
im = im + 10*rand(size(im));
% display image
figure
subplot(2,2,1)
image(im)
colormap gray
% segment image based on intensity
bottomThird = 256/3;
topThird = 2*256/3;
index1 = find(im < bottomThird);
index2 = find(and((im > bottomThird),(im <topThird)));
index3 = find(im > topThird);
%create images for each segmented region
im1 = ones(size(im));
im2 = ones(size(im));
im3 = ones(size(im));
im1(index1) = 0;
im2(index2) = 0;
im3(index3) = 0;
%display sub-regions
subplot(2,2,2)
imagesc(im1)
subplot(2,2,3)
imagesc(im2)
subplot(2,2,4)
imagesc(im3)

How do I display the red channel of an image in Matlab?

I have a 3D matrix im which represents an RGB image. I can do
imshow(im)
to display the image.
I want to display only one of the RGB channels at a time: I want to display the red channel and I want it to appear red.
I've tried
imshow(im(:,:,1))
but it displays the grayscale image (which is not what I want).
How do I display the red channel and make it appear red?
I have three proposals for you.
1.
Use the imagesc function and choose a red color palette.
2.
Clear the other color channels: im(:,:,2:3) = 0; imshow(im);
3. Use the ind2rgb function with a color map you build accordingly.
Try this:
% display one channel only
clear all;
im=imread('images/DSC1228L_512.jpg');
im_red = im;
im_green = im;
im_blue = im;
% Red channel only
im_red(:,:,2) = 0;
im_red(:,:,3) = 0;
figure, imshow(im_red);
% Green channel only
im_green(:,:,1) = 0;
im_green(:,:,3) = 0;
figure, imshow(im_green);
% Blue channel only
im_blue(:,:,1) = 0;
im_blue(:,:,2) = 0;
figure, imshow(im_blue);
Try this
I = imread('exemple.jpg');
%Red component
R = I(:,:,1);
image(R), colormap([[0:1/255:1]', zeros(256,1), zeros(256,1)]), colorbar;
%Green Component
G = I(:,:,2);
figure;
image(G), colormap([zeros(256,1),[0:1/255:1]', zeros(256,1)]), colorbar;
%Blue component
B = I(:,:,3);
figure;
image(B), colormap([zeros(256,1), zeros(256,1), [0:1/255:1]']), colorbar;
You mean you want to extract red color only?
using im(:,:,1) only seperate the red channel from the 3D image and convert it to a 2D image.
Try this simple code:
im=imread('example.jpg');
im_red=im(:,:,1);
im_gray=rgb2gray(im);
im_diff=imsubtract(im_red,im_gray);
imshow(im_diff);
For a better view, you could calculate and display the pure color. The formula Rp = Rc / (Rc + Gc + Bc). And a code example for the color red:
imagesc(im(:,:,1) ./ (im(:,:,1) + im(:,:,2) + im(:,:,3)))
This will make the color display more clearly, since the other colors have been filtered out.
I will try to illustrate it with an example:
Original image:
Red channel of image (im(:,:,1)):
Pure red:

Resources