Am trying to create transparent mat but am getting black background image in return so any idea how to do it as i have to overlay this image with some content on camera video feed.
code i tried.
cv::Mat comp = cv::Mat::zeros( currentImage.size(), CV_8UC4 );
comp.setTo(cv::Scalar(0,0,0,0));
imshow( "transparent", comp ); // show black background image.
thanks to #HansHire and #Scheff we find out that problem is in cv::imshow() it not render transparency so to check Mat for transparency it need to dumped to disk to be checked using system image viewer which render transparency very well
cv::Mat comp = cv::Mat::zeros( currentImage.size(), CV_8UC4 );
comp.setTo(cv::Scalar(0,0,0,0));
// imshow( "transparent", comp ); // show black background image because it cant render transparency .
imwrite( "C:/opencv_dump.png", comp ); // transparent upon opening , thumbnail may look black.
Related
I have one transparent SVG image, I am trying to generate the thumbnail of that image. It generates a thumbnail of the image properly, but it removes the transparency of the image and adds a black background to the image. I want to generate thumbnail without losing the transparency of the image.
Note - The same problem with jpg and jpeg images also, but I added white background to the image in svg case it's not make the transparent background
I am using PHP vips library.
Here is my code for that
$im = Vips\Image::thumbnail($filename, 180, ['height' => 225, 'size' => 'both']);
$alpha = $im->hasAlpha();
if($alpha) {
$im = $im->flatten(['background' => [255, 255, 255]]);
}
Just write to a format that supports transparency, like PNG.
For example:
$im = Vips\Image::thumbnail("x.svg", 180, ['height' => 225, 'size' => 'both']);
$im->WriteToFile("x.png");
WEBP, GIF, HEIC, AVIF, TIFF etc. also support transparency. JPEG does not.
I am using vtkResliceImageViewer vtk library class to display DICOM files. Images are too dark. I don't know if I use it wrong way or it's a bug (there was similar unresolved problem posted before: Correct display of DICOM images ITK-VTK (images too dark)).
I am using custom DICOM data reader and I'm sure data is ok. vtkImageViewer2 class displays it correctly. I'am sure that this is not issue connected to Modality LUT, Rescale Slope/Intercept, VOI LUT and window center/width (values are fine).
I attach basic code example producing two images. Window center = 40 and width = 400 are hardcoded and are 100% proper for tested image. Render window with red background displays image using vtkImageViewer2 class and it's perfectly fine. Render window with green background display image using vtkResliceImageViewer and it's too dark. When I set WindowLevel to -40 instead of 40 it's look close to the proper displayed image.
Both clases use vtkImageMapToWindowLevelColors to apply window transformation. I've tried using SetOutputFormatToLuminance() and PassAlphaToOutputOff() methods of that class but this didn't help.
Does somebody have same problem and resolved it some way?
SeriesDataReader dataReader;
vtkSmartPointer<vtkImageData> data = dataReader.ReadSeriesData();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(900, 900);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(0, 1, 0);
renderer->ResetCamera();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindow> renderWindow2 = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow2->SetSize(900, 900);
vtkSmartPointer<vtkRenderer> renderer2 = vtkSmartPointer<vtkRenderer>::New();
renderer2->SetBackground(1, 0, 0);
renderer2->ResetCamera();
renderWindow2->AddRenderer(renderer2);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor2 = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor2->SetRenderWindow(renderWindow2);
vtkSmartPointer<vtkImageViewer2> imageViewer = vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputData(data);
imageViewer->SetColorLevel(40.0);
imageViewer->SetColorWindow(400.0);
imageViewer->SetupInteractor(renderWindowInteractor2);
imageViewer->SetRenderWindow(renderWindow2);
imageViewer->SetRenderer(renderer2);
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
imageViewer->Render();
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkResliceImageViewer> resliceImageViewer = vtkSmartPointer<vtkResliceImageViewer>::New();
resliceImageViewer->SetupInteractor(renderWindowInteractor);
resliceImageViewer->SetRenderWindow(renderWindow);
resliceImageViewer->SetSliceOrientationToXY();
resliceImageViewer->SetRenderer(renderer);
resliceImageViewer->SetInputData(data);
resliceImageViewer->SetSliceOrientation(2);
resliceImageViewer->SetResliceModeToAxisAligned();
resliceImageViewer->SetColorLevel(40.0);
resliceImageViewer->SetColorWindow(400.0);
resliceImageViewer->SetResliceMode(0);
resliceImageViewer->GetRenderer()->ResetCamera();
resliceImageViewer->Render();
imageViewer->GetRenderWindow()->Render();
renderWindowInteractor2->Initialize();
imageViewer->GetRenderWindow()->Render();
renderWindowInteractor2->Start();
resliceImageViewer->GetRenderWindow()->Render();
renderWindowInteractor->Initialize();
resliceImageViewer->GetRenderWindow()->Render();
renderWindowInteractor->Start();
I solved that issue. For proper display of images with wl/ww transformation comment out or remove line 327 in void vtkResliceImageViewer::InstallPipeline() function.
Line 327: this->WindowLevel->SetLookupTable(this->GetLookupTable());
I am trying to display an image (with an alpha value) in a window (of size 200X200). The image is loaded into an OpenCV Mat datastructure.
The image I am loading, looks like this:
The image i see when displayed, looks like this:
The code I am using to load the Bitmap is as follows:
NSBitmapImageRep* imageRep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
pixelsWide:200
pixelsHigh:200
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:(200*4)
bitsPerPixel:32] autorelease];
memcpy(imageRep.bitmapData,paintBuffer.data,160000);
NSSize imageSize = NSMakeSize(200,200);
NSImage* myImage = [[[NSImage alloc] initWithSize: imageSize] autorelease];
[myImage addRepresentation:imageRep];
g_imageView setImage:myImage];
Update:
I was initially reading BGRA instead of the expected RGBA. On changing that, I just get a red version of image above, like this:
I've been scratching my head for a few days on how to complete the task of making the edges rounded on an image taken from picamera using python-wand. I have it setup now to where it grabs the image and composites it over the banner/background image with the following:
img = Image(filename=Picture)
img.resize(1200, 800)
bimg = Image(filename=Background)
bimg.composite(img, left=300, top=200)
bimg.save(filename=BPicture)
Any help is appreciated!
You can use wand.drawing.Drawing.rectangle to generate rounded corners, and overlay it with composite channels.
from wand.image import Image
from wand.color import Color
from wand.drawing import Drawing
with Image(filename='rose:') as img:
img.resize(240, 160)
with Image(width=img.width,
height=img.height,
background=Color("white")) as mask:
with Drawing() as ctx:
ctx.fill_color = Color("black")
ctx.rectangle(left=0,
top=0,
width=mask.width,
height=mask.height,
radius=mask.width*0.1) # 10% rounding?
ctx(mask)
img.composite_channel('all_channels', mask, 'screen')
img.save(filename='/tmp/out.png')
Now if I understand your question, you can apply the same technique, but composite Picture in the drawing context.
with Image(filename='rose:') as img:
img.resize(240, 160)
with Image(img) as nimg:
nimg.negate() # For fun, let's negate the image for the background
with Drawing() as ctx:
ctx.fill_color = Color("black")
ctx.rectangle(left=0,
top=0,
width=nimg.width,
height=nimg.height,
radius=nimg.width*0.3) # 30% rounding?
ctx.composite('screen', 0, 0, nimg.width, nimg.height, img)
ctx(nimg)
nimg.save(filename='/tmp/out2.png')
I plan to do a stereo image from the tutorial here, but the compiler reports errors with cv2.createStereoBM, I found out it was the problem of OpenCV version.
I followed this to change cv2.createStereoBM into cv2.StereoBM. It works well, but the following code:
disparity = stereo.compute(frame0,frame1)
shows error:
Both input images must have CV_8UC1 in function cv::findStereoCorrespondenceBM
Can anyone help me with this?
The environment is Python 2.7, OpenCV 2.4.11.
My code is:
cap0 = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
while (cap0.isOpened() and cap1.isOpened()):
ret0, frame0 = cap0.read()
frame0_new=cv2.cvtColor(frame0, cv2.COLOR_BGR2GRAY)
ret1, frame1 = cap1.read()
frame1_new=cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET,ndisparities=16, SADWindowSize=15)
disparity = stereo.compute(frame0,frame1)
You should use your frames converted to single channel, i.e. of type CV_8UC1:
disparity = stereo.compute(frame0_new, frame1_new)