i have raw image buffer. i am converting it into jpeg using following code:
height = 240;
width = 320;
raw_image=capture(width, height);//(c code uvc capture)
QImage tmpImage = QImage(raw_image, width, height, QImage::Format_RGB32 ); //image.format=RGB888
QByteArray im;
QBuffer bufferJpeg(&im);
bufferJpeg.open(QIODevice::WriteOnly);
tmpImage.save(&bufferJpeg, "JPG");
tmpImage.save("image1.jpg","JPG");
it is capturing and converting it into jpeg.but the captured image is not proper. i have attached the image for reference
and for QImage tmpImage = QImage(raw_image, width, height, QImage::Format_RGB16 ); image is
for QImage tmpImage = QImage(raw_image, width, height, QImage::Format_RGB444);
How can i get the proper image? thanks in advance.
wrong color space.
==> Format_RGB32
Try:
QVideoFrame::Format_YUYV or
QVideoFrame::Format_UYVY
Related
I have to load images:
Image image = new Image(f.toURI().toString(), width, height, true, true);
Since I also have tiffs, I have to load them differently using JAI:
BufferedImage read = ImageIO.read(f);
Image image = SwingFXUtils.toFXImage(read, null);
Now I have an Image object, but it has the wrong size. Image offers no methods to resize the object. How do I resize is so it has the same size as if I would load a jpg or png with the shown line?
Seems like there should be an easier way to do this, but you can try:
BufferedImage read = ImageIO.read(f);
int[] pixels = new int[width * height] ;
PixelGrabber grabber = new PixelGrabber(read.getScaledInstance(width, height, Image.SCALE_SMOOTH),
0, 0, width, height, 0, pixels, width);
grabber.grabPixels();
WritableImage fxImage = new WritableImage(width, height);
PixelWriter pw = fxImage.getPixelWriter();
pw.setPixels(0, 0, width, height, PixelFormat.getIntArgbInstance(), pixels, 0, width);
Now fxImage is a javafx.scene.image.Image that has dimensions width by height.
Be aware that PixelGrabber.grabPixels() is a blocking call, so you will need to handle InterruptedExceptions. For large images you might want to do this in a Task executed on a background thread, so as not to block the FX Application Thread.
I have a QGraphicScene object that I can save to a PNG file just fine as long as I haven't made any modifications like a scroll or a zoom on the item. After performing a scroll or a zoom, the saved image becomes small with a lot of transparent pixels filling in the desired dimensions. I'm thinking it has to do with needing to render the visible region of the QGraphicsView to the QImage that gets saved but I'm having trouble figuring this out.
Below is some of my code:
void myClass::saveSceneAsPNGImage(QString path, int width)
{
Scene->clearSelection();
double scaleFactor = (width / Scene->sceneRect().size().width());
QImage image(Scene->sceneRect().size().width() * scaleFactor,
Scene->sceneRect().size().height() * scaleFactor,
QImage::Format_ARGB32);
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
Scene->render(&painter);
image.save(path);
}
I have developed a small GUI in C++/Qt, I would like to know a fast way to test if image loaded is grayscale. In practise, when I load a grayscale image of gif format, I want it to be recognized as a grayscale image with depth()=8, and when I load a colored gif image, the depth of QImage would be 32.
Here's my open method :
void ImageViewer::open()
{
int status_off = 0;
fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty()) {
current_qimage = QImage(fileName);
if (current_qimage.isNull()) {
QMessageBox::information(this, tr("Image Viewer"),
tr("Cannot load %1.").arg(fileName));
return;
}
updateStatus(status_off);
// Image is colored with depth=8
if (current_qimage.depth()/8 != 4)
{rgblum_status = 0;}
else // Image is grayscale with depth=32
{rgblum_status = 1;}
loadImage();
}
}
From my first test, it seems that current_qimage, in current_qimage = QImage(fileName); inherits firstly from the format (gif here) before the contents of the image. Therefore, QImage has in two cases a depth() equal to 32.
How to make the difference between these two gif images (one grayscale and the other colored) ?
The QImage class has a function that you can call to test if the image is grayscale or not: QImage::isGrayscale(). It works for both 8-bit color table indexed images and 32-bit images.
I want to use an ImageView to resize an image, however the image is not being resized:
ImageView imageView = new ImageView(image);
imageView.setPreserveRatio(true);
imageView.setFitHeight(40);
System.out.println("imageview image width = " + imageView.getImage().getWidth());
System.out.println("imageview image height = " + imageView.getImage().getHeight());
The output is
imageview image width = 674.0
imageview image height = 888.0
However, the width should be 40. My ImageView is not attached to any scene and I also don't want to attach it, it shall only be used for image resizing. Is there any way to force the ImageView to resize its image, even though the ImageView is not attached to any scene? The reason I am using an ImageView for resizing is, that I want to resize an Image in RAM, without reading it again from the disk, please see this question for more details.
Thanks for any hint!
Using an ImageView for resizing seems to be very hacky.
A better approach is to convert your Image into a BufferedImage and do the resizing the old way. (JavaFx does not (yet) provide an internal way to resize memory images)
int width = 500; // desired size
int height = 400;
Image original = ...; // fx image
BufferedImage img = new BufferedImage(
(int)original.getWidth(),
(int)original.getHeight(),
BufferedImage.TYPE_INT_ARGB);
SwingFXUtils.fromFXImage(original, img);
BufferedImage rescaled = Scalr.rescaleImage(img, width, heigth); // the actual rescale
// convert back to FX image
WritableImage rescaledFX = new WritableImage(width, heigth);
SwingFXUtils.toFXImage(rescaled, rescaledFX);
Where as Scalr is a nice library for resizing images in native java. Obviously, you can use other/simpler methods of rescaling, but the image quality won't be that nice.
I have been trying to display a gray-scale image using Qt. The image data is loaded from a .txt file that contains 256x256 float data. There is no header involved for the image. I have tried the solution posted in this link
I used QLabel class to call setPixmap of my uchar* image_data_array. Even though I could get a Qt GUI window to open, but the window shows just blank screen.
imageLabel -> setPixmap(QPixmap::fromImage(*myImage));
QImage img = AImage;
if (!AImage.isNull())
{
int pixels = img.width() * img.height();
if (pixels*(int)sizeof(QRgb) <= img.byteCount())
{
QRgb *data = (QRgb *)img.bits();
for (int i = 0; i < pixels; i++)
{
int val = qGray(data[i]);
data[i] = qRgba(val, val, val, qAlpha(data[i]));
}
}
}
return img;
Use RGBA for good grayscale.
How are you loading the QImage with the grey image data ?
QT doesn't have a greyscale image type, only a bilevel type. You can either create an RGB image with R=G=B=grey. Or more preferably use QImage::Format_Indexed8 and create a colour table where each entry has the same value as the index. i.e.
QImage *qi = new QImage(data_ptr, width, height, QImage::Format_Indexed8);
QVector<QRgb> my_table;
for(int i = 0; i < 256; i++) my_table.push_back(qRgb(i,i,i));
qi->setColorTable(my_table);
In QT 5.9.1 there is an option to create a grayscale image:
QImage *qi = new QImage(data_ptr, width, height, QImage::Format_Grayscale8);