OpenCV C++ resize() not found - xcode

I installed opencv 2.4.9 for macOS and integrated it with Xcode. However, although it finds most functions, when calling the resize() function, I get the build error 'Use of undeclared identifier resize'.
Can anybody please tell me how to fix this?

You don't mention how you are calling it, but there are two resize functions: a member of Mat that changes the number of rows, and cv::resize() that interpolates to resize an image. For the latter you need imgproc.hpp.
#include <opencv2/imgproc/imgproc.hpp>
//...
cv::resize(src, dst, dst.size(), 0, 0, interpolation);

Related

Text rendered as blobs on mono images in qt5

I am trying to "print" items that contain text, onto a mono image.
I have items that either subclass QGraphicsTextItem, or some other class that uses painter->drawText().
I can do it in Qt4 - but in Qt5, all I get are some blobs. I have tried to experiment with a small sample program, but I can't figure out what is going on...
Here is a simple self contained program:
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsTextItem>
class TextItem : public QGraphicsItem
{
public:
TextItem(){}
virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget* = NULL)
{
QPen p(Qt::red);
painter->setPen(p);
painter->setBrush(Qt::NoBrush);
//painter->setFont(QFont("Arial", 30));
painter->drawText(boundingRect(), Qt::AlignCenter, "abcd");
}
virtual QRectF boundingRect() const { return QRectF(0, 0, 30, 20); }
};
void processScene(QGraphicsScene* s) {
QGraphicsTextItem* t = new QGraphicsTextItem();
t->setPlainText("abcd");
// t->setDefaultTextColor(Qt::red); // won't make a difference
t->setPos(0, 0);
s->addItem(t);
//t->setFont(QFont("Arial", 30));
//t->setTransform(t->transform().fromScale(3,3));
TextItem* t1 = new TextItem();
t1->setPos(0, 20);
s->addItem(t1);
QImage image = QImage(s->sceneRect().size().toSize(),
QImage::Format_Mono);
//QImage::Format_RGB32); // this works in qt5
image.fill(QColor(Qt::color0).rgb());
QPainter painter;
painter.begin(&image);
s->render(&painter);
painter.end();
image.save(QString("../test.bmp"));
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QGraphicsScene s;
s.setSceneRect(0, 0, 50, 50);
QGraphicsView view(&s);
view.show();
processScene(&s);
return app.exec();
}
Running it in Qt4 it gives what is expected: outputs a mono image containing text (in both versions, using QGraphicsTextItem or paint->drawText() (left image).
The same in Qt5 only shows some blobs in place of text (right image):
I have tried all kinds of experiments... Setting the font doesn't make a difference (except, for QGraphicsTextItem, setting the font to something big AND scaling to something big - but that is not useful in a practical application).
The other thing that worked in Qt5 is rendering to a color image instead of mono - but that is very bad for my application, where I need to store small mono images. My thought would be then, for Qt5, to render text to a color bitmap and then convert it to mono... But really !
I have also tried to save the items themselves to a bitmap, in a function implemented inside their class - it did the same thing.
I have tried to set point size on the QFont before applying it.
Is there something else I can set on QPainter in order to draw text ?
I found a related question: QT5 Text rendering issue also asked again in Qt forums: QT5 Text rendering issue. But that question refers to all rendering, and to a Qt5 in Alpha on a MIPS based platform... It implies that other platforms don't see that issue...
I'm using 5.5.0... I am also running under windows... And rendering on color image works... And...
I simply do not understand the "solution":
For time being, when we tried forcefully to enable Glyph cache, then
we are not seeing this issue.
Not sure what that means and how to do it.
Why is text painting blobs on mono images (but fine on color images) in qt5 and what can I do to fix it ?
Using Qt 5.5.0 32 bit, in Windows 7
Edit:
1) I tested the code in 5.4.2 and it renders text correctly.
I tried to compare some of the relevant code in qpainter.cpp, qtextlayout.cpp, qpaintengine_raster.cpp, qtextengine.cpp
- but with my limited understanding, the changes between Qt 5.4.2 and Qt 5.5.0 look logical... and would not affect the behavior...
2) I did paint my text on a color QImage, then painted it on mono - it worked fine on my own text items, but a third party software that creates very small text gives me very bad resolution when I try the same (illegible).

How to fix my deprecated use of gluOrtho2D()?

I've got existing code (OS X, Obj-C, NSOpenGLView) that calls:
gluOrtho2D(0.0, newSize.width, newSize.height, 0.0);
It works fine except that I get a deprecated function warning that urges me to use GLKMatrix4MakeOrtho() instead. OK – but how? I can't seem to even find the existence of that function; I'm including:
#import <OpenGL/gl.h>
#import <OpenGL/glu.h>
and Xcode does not know of a function by that name. My OpenGL reference manual does not have any mention of it, or indeed, of any functions with the prefix GLK; what's going on there? And then if I managed to find the function and include the right header and link against whatever I need to link against, what then – what would an equivalent call be for GLKMatrix4MakeOrtho() that would do the same thing as my gluOrtho2D() call? I tried Googling, and found many hits showing that other people are getting the same deprecation warning, but I couldn't find anybody saying how to fix it...
The include you need for GLKMatrix4MakeOrtho() is:
#include <GLKit/GLKMatrix4.h>
Then you call the function with the same arguments you would use with glOrtho(), e.g.:
GLKMatrix4 orthoMat = GLKMatrix4MakeOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
How you use it depends on the kind of OpenGL you use. For fixed function with the legacy matrix stack, you can use for example:
glLoadMatrix(orthoMat.m);
With the programmable pipeline, you would typically load it as a uniform:
glUniformMatrix4fv(loc, 1, GL_FALSE, orthoMat.m);
Wow, that's a really unusual recommendation. GLKMatrix4MakeOrtho() is not a drop-in replacement for gluOrtho2D(); it's a function that would be used here if you were porting your application to OpenGL 3.2 or later. However, that port would be a much bigger task than just changing out this one call, as OpenGL 3.2 does not support any of the immediate mode APIs from earlier versions of OpenGL (e.g, glBegin()).
Bottom line is, so long as you continue to use OpenGL 1.x/2.x APIs, you will need to ignore this warning.
Here is one alternative to disable OpenGL deprecation warnings on macOS 10.15 and Xcode 11.2.1:
main.c
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
Another alternative would be via Build Settings:

OpenCV cv::Mat displays empty gray images? Cant find the reason

I just want to display this "img1.jpg" image in a c++ project with using opencv libs for future processes, but it only displays an empty gray window. What is the reason of this. Is there a mistake in this code? please help!
Here is the code;
Mat img1;
char imagePath[256] = "img1.jpg";
img1 = imread(imagePath, CV_LOAD_IMAGE_GRAYSCALE);
namedWindow("result", 1);
imshow("result", img1);
Thanks...
I had the same problem and solved putting waitKey(1); after imshow(). The OpenCV documentation explains why:
This function is the only method in HighGUI that can fetch and handle
events, so it needs to be called periodically for normal event
processing, unless HighGUI is used within some environment that takes
care of event processing.
Thanks #b_froz.
For more detials about this issue,you can refer to: http://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html#imshow
Note This function should be followed by waitKey function which displays the image for specified milliseconds. Otherwise, it won’t display the image. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame for 25 ms, after which display will be automatically closed. (If you put it in a loop to read videos, it will display the video frame-by-frame)
So,not only waitkey(1)could be put after imshow(),but also waitkey(0) or waitkey(other integers).Here is the explanation of the function waitkey() : http://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html#waitkey
Are you importing the correct library ?
This is other very easy way to load one image:
#define CV_NO_BACKWARD_COMPATIBILITY
#include <cv.h>
#include <highgui.h>
#include <math.h>
main(){
IplImage* img = cvLoadImage("yourPicture.jpg");
cvNamedWindow("Original", 1);
cvShowImage("Original", img);
}
I think you have openCV correctly installed, so yo can type this (Ubuntu):
g++ NameOfYourProgram.cpp -o Sample -I/usr/local/include/opencv/ -L/usr/local/lib -lcv -lhighgui ./sample
The problem you are having is due to the type of your Mat img1. When you load your image with the flag CV_LOAD_IMAGE_GRAYSCALE, the type of your Mat is 0 (CV_8UC1), and the function imshow() is not able to show the image correctly.
You can solve this, converting your Mat to type 16 (CV_8UC3):
img1.convertTo(img1,CV_8UC3);
and then show it with imshow():
imshow("result", img1);
I hope this help.

OpenCV: Drawing on an image

I am working on a program using the OpenCV library (though I am quite a noob on it). One of the things I need to do is to draw on the image. I looked at the OpenCV drawing functions and they all seem pretty simple (Circle, Line, etc), however the program won't compile! It says this to be exact: error C3861: 'Line': identifier not found.
Is there something I haven't installed? I used the tutorial on http://opencv.willowgarage.com/wiki/VisualC%2B%2B_VS2008 to install OpenCV on Visual Studio 2008 and so far this is the only real problem I have.
Please help me! I need this program working as soon as possible!
The function to draw a line in the OpenCV C API is named cvLine, not Line.
I think you have fallen victim of the following common mistake:
C includes are in #include <opencv/core.h> etc, whereas
C++ includes are:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <oppencv2/highgui/highgui.hpp>
Include these for drawing and showing the image. Use using namespace cv; then
you don't have to write cv::line just line and everything will be working fine.
I had to battle with the very same problem when I began. ;)
(And btw use cv::Mat for c++.)
You can now easily paint on OpenCV images. For this you need to call the setMouseCallback(‘window_name’,image_name) function on opencv. After that you can easily handle the Mouse Callback Function upon your images. Then you need to detect the cv2.EVENT_LBUTTONDOWN, cv2.EVENT_MOUSEMOVE and cv2.EVENT_LBUTTONUP events. By checking the proper boolean condition you need to decide how you like to interact with the OpenCV images.
def paint_draw(event,former_x,former_y,flags,param):
global current_former_x,current_former_y,drawing, mode
if event==cv2.EVENT_LBUTTONDOWN:
drawing=True
current_former_x,current_former_y=former_x,former_y
elif event==cv2.EVENT_MOUSEMOVE:
if drawing==True:
if mode==True:
cv2.line(image,(current_former_x,current_former_y),(former_x,former_y),(0,0,255),5)
current_former_x = former_x
current_former_y = former_y
elif event==cv2.EVENT_LBUTTONUP:
drawing=False
if mode==True:
cv2.line(image,(current_former_x,current_former_y),(former_x,former_y),(0,0,255),5)
current_former_x = former_x
current_former_y = former_y
return former_x,former_y
For details you can see link: How to Paint on OpenCV Images and Save the Image
Output:

Problem in displaying sequence of DICOM images using QT

i'm working in Linux [GCC Compiler] ,
i'm using Eclipse with CDT + QT to compile
I need to display sequence of DICOM images using QT window and OpenGL functions
pls let me know which is the function to display sequence of images
i'm using 3 functions
1) initiallizeGL() to initallize OpenGL functions.
2) resizeGL() instead of glutInitWindowSize() in Glut.
3) paintGL() instead of glutDisplayFunc() in Glut.
4) updateGL() instead of glutPostRedisplay() in Glut.
also pls let me know which are the Glut equivalent functions in QT
glutMainLoop();
glutSwapBuffers();
glutInitDisplayMode();
glutIdleFunc(idle);
glutInit(&argc, argv);
You should be able to easily display images by simply using QGLWidget as your painting device which - depending on your specific usecase - might simplify your implementation. This will draw the image using the OpenGL paint engine in Qt. Something like the following should allow you to display an image;
class CustomWidget : public QGLWidget
{
public:
CustomWidget(QWidget* parent=0) : QGLWidget(parent), pix("foo.jpg")
{
}
protected:
void paintEvent(QPaintEvent *pe)
{
QPainter p(this);
// maybe update the pixmap
p.drawPixmap(this->rect(),pix);
}
private:
QPixmap pix;
};
If you need to put it in a 3D scene, you probably need to load the image as a texture. Some of the Qt OpenGL demos should be able to give you a starting point, e.g. the 'Boxes' demo;
http://doc.trolltech.com/4.6-snapshot/demos-boxes.html
I'd say Qt already take care of gluSwapBuffers, glutInitDisplayMode and glutInit, so you dont need those.
I am also not sure if your mapping of functions is correct, simply put, Qt and Glut think about GL in a different way so possibly you have to to the same (think in a different way) and Qt methods are pretty much self explanatory.
I'd recommend download the code of KGLEngine or any other Qt +GL project to better understand how it works.

Resources