Ask Your Question
0

How to add colored overlay onto image

asked 2017-12-21 10:05:58 -0500

Nilushika gravatar image

updated 2017-12-21 11:27:01 -0500

image description first image image description second image

I have a gray-scale image of a lung CT scan slice (first image). After carrying some image processing steps I have ended up with some interested areas on original image as a binary image (second image). Those interested areas are in white on the binary image. I want to show red colored (semi-transparent) overlay of these interested areas on the top of the original image.I tried using alpha blending and imshow(). But didn't work. Can you please help me,

edit retag flag offensive close merge delete

Comments

If image sizes are equal then you can create color images using cvtColorwith [code] for second image(https://docs.opencv.org/trunk/d... and convert first image using three planes (two plane with zeros and last plane with mask). Then add two images

LBerger gravatar imageLBerger ( 2017-12-21 10:16:54 -0500 )edit

Thanks LBerger. But which color code to use for the conversion of binary to color(RGB) with cvtColor

Nilushika gravatar imageNilushika ( 2017-12-21 11:34:37 -0500 )edit

I cropped your one image so that both images were the same size. If you're not too concerned about speed, you can try this Python code:

import cv2
import numpy

lung = cv2.imread('lung1.jpg')
overlay = cv2.imread('lung2.jpg')
overlay = cv2.cvtColor(overlay, cv2.COLOR_BGR2GRAY)

if lung is None or overlay is None:
    print('Error loading images')
    exit()

for j in range(0, lung.shape[0]):
    for i in range(0, lung.shape[1]):
        if 255 == overlay[j, i]:
            lung_r = lung[j, i, 2]
            lung_r += 255
            lung_r /= 2
            lung[j, i, 2] = lung_r

cv2.imshow('frame', lung)

cv2.waitKey(0)
sjhalayka gravatar imagesjhalayka ( 2017-12-21 12:19:41 -0500 )edit

Does this help u...cv2.addWeighted

supra56 gravatar imagesupra56 ( 2017-12-21 13:54:06 -0500 )edit

maybe you can find this useful

sturkmen gravatar imagesturkmen ( 2017-12-21 14:07:25 -0500 )edit

Thanks for replying sjhalayka. But does your code result in colored overlay, the first image I have is a 2d numpy array of dtype int16

Nilushika gravatar imageNilushika ( 2017-12-21 23:59:01 -0500 )edit

Thanks sturkmen , the link was really helpful, but I had problems with conversion of gray image to BGR color image.

Nilushika gravatar imageNilushika ( 2017-12-22 00:00:14 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-12-21 15:05:08 -0500

LBerger gravatar image

try something like this

struct ParamThresh {
    int threshMin;
    String winName;
    Mat img;
};

void AjouteGlissiere(String nomGlissiere, String nomFenetre, int minGlissiere, int maxGlissiere, int valeurDefaut, int *valGlissiere, void(*f)(int, void *), void *r)
{
    createTrackbar(nomGlissiere, nomFenetre, valGlissiere, 1, f, r);
    setTrackbarMin(nomGlissiere, nomFenetre, minGlissiere);
    setTrackbarMax(nomGlissiere, nomFenetre, maxGlissiere);
    setTrackbarPos(nomGlissiere, nomFenetre, valeurDefaut);
}

void ChooseThreshold(int x, void *r)
{
    ParamThresh *pgc = (ParamThresh*)r;
    if (!pgc->img.empty() )
    {
        Mat imgThresh;
        threshold(pgc->img, imgThresh, pgc->threshMin, 255, THRESH_BINARY);
        imshow("thresh", imgThresh);
        imshow("original", pgc->img);
        Mat imgColor, imgThreshColor;
        vector<Mat> p;
        p.push_back(Mat::zeros(pgc->img.size(), CV_8UC1));
        p.push_back(pgc->img);
        p.push_back(Mat::zeros(pgc->img.size(),CV_8UC1));
        merge(p, imgColor);
        cvtColor(pgc->img&imgThresh, imgThreshColor, COLOR_GRAY2BGR);
        Mat dst;
        addWeighted(imgColor, 0.15, imgThreshColor, 0.85, 0, dst);
        imshow(pgc->winName, dst);
        waitKey(50);
    }
}

int main(int argc, char *argv[])
{
    ParamThresh pgc;
    pgc.winName = "result";
    pgc.img = imread("C:/Users/Laurent.PC-LAURENT-VISI/Desktop/15138717637485638.jpg", IMREAD_GRAYSCALE);
    namedWindow(pgc.winName, WINDOW_GUI_EXPANDED);
    AjouteGlissiere("thresh", pgc.winName, 0, 255, 128, &pgc.threshMin, ChooseThreshold,(void*)&pgc);
    char code = 0;
    while(code!=27)
        code=waitKey(10);
    return 0;
}
edit flag offensive delete link more

Comments

Thank you LBerger. But I am getting distorted source image when it converted to BGR color image using cvtColor with the conversion COLOR_GRAY2BGR. For the binary image(second) conversion was successful. But for the source image(first) it distorted. The source image is a 2d array of type int16. I converted it to float32

Nilushika gravatar imageNilushika ( 2017-12-22 00:03:47 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-12-21 10:05:58 -0500

Seen: 4,338 times

Last updated: Dec 21 '17