#include "mark_system.h"
QString image_path = "UI/image/";
Mark_System::Mark_System(QWidget *parent)
: QMainWindow(parent)
{
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
ui.setupUi(this);
connect(ui.btn_Origin,SIGNAL(clicked()),this,SLOT(slot_Origin()));
connect(ui.btn_GrayScale,SIGNAL(clicked()),this,SLOT(slot_GrayScale()));
connect(ui.btn_Binaryzation,SIGNAL(clicked()),this,SLOT(slot_Binaryzation()));
connect(ui.btn_Save,SIGNAL(clicked()),this,SLOT(slot_Save()));
root_widget = NULL;
image = NULL;
image_widget_label =NULL;
root_widget = new QWidget(this);
//这里用电脑屏幕做测试,所以写死0,0,实际上用rootwidget.location.x和rootwidget.location.y
root_widget->move(0,0);
root_widget->resize(500,375);
root_widget->setAutoFillBackground(true);
QPalette palette;
palette.setColor(QPalette::Background, QColor(0,0,0,0));
root_widget->setPalette(palette);
image_widget_label = new QLabel(root_widget);
image_widget_label->move(0, 0);
//image_widget_label->setPixmap(QPixmap(image_path + "love.png"));
image = new QImage(500,375,QImage::Format_Indexed8);
QString filename = QFileDialog::getOpenFileName(this,tr("选择图片"),"",tr("Images (*.bmp *.png *.jpg)"));
image->load(filename);
image_widget_label->setPixmap(QPixmap::fromImage(*image));
}
Mark_System::~Mark_System()
{
}
void Mark_System::slot_Origin()
{
image_widget_label->setPixmap(QPixmap::fromImage(*image));
}
void Mark_System::slot_GrayScale()
{
int height = image->height();
int width = image->width();
QImage ret(width, height, QImage::Format_Indexed8);
ret.setColorCount(256);
for(int i = 0; i < 256; i++)
{
ret.setColor(i, qRgb(i, i, i));
}
switch(image->format())
{
case QImage::Format_Indexed8:
/*for(int i = 0; i < height; i ++)
{
const uchar *pSrc = (uchar *)image->constScanLine(i);
uchar *pDest = (uchar *)ret.scanLine(i);
memcpy(pDest, pSrc, width);
} */
break;
case QImage::Format_RGB32:
case QImage::Format_ARGB32:
case QImage::Format_ARGB32_Premultiplied:
for(int i = 0; i < height; i ++)
{
const QRgb *pSrc = (QRgb *)image->constScanLine(i);
uchar *pDest = (uchar *)ret.scanLine(i);
for( int j = 0; j < width; j ++)
{
pDest[j] = qGray(pSrc[j]);
}
}
break;
}
image_widget_label->setPixmap(QPixmap::fromImage(ret));
}
void Mark_System::slot_Binaryzation()
{
int height = image->height();
int width = image->width();
QImage ret(width, height, QImage::Format_Indexed8);
ret.setColorCount(256);
for(int i = 0; i < 256; i++)
{
ret.setColor(i, qRgb(i, i, i));
}
switch(image->format())
{
case QImage::Format_Indexed8:
/*for(int i = 0; i < height; i ++)
{
const uchar *pSrc = (uchar *)image->constScanLine(i);
uchar *pDest = (uchar *)ret.scanLine(i);
memcpy(pDest, pSrc, width);
} */
break;
case QImage::Format_RGB32:
case QImage::Format_ARGB32:
case QImage::Format_ARGB32_Premultiplied:
unsigned long tmp = 0;
for(int i = 0; i < height; i ++)
{
const QRgb *pSrc = (QRgb *)image->constScanLine(i);
uchar *pDest = (uchar *)ret.scanLine(i);
for( int j = 0; j < width; j ++)
{
pDest[j] = qGray(pSrc[j]);
tmp +=pDest[j];
}
}
if(ui.binaryzation_Value->text().toInt() > 0)
tmp = ui.binaryzation_Value->text().toInt();
else
tmp = tmp/(height*width);
qDebug()<<tmp;
for(int i = 0; i < height; i ++)
{
const QRgb *pSrc = (QRgb *)image->constScanLine(i);
uchar *pDest = (uchar *)ret.scanLine(i);
for( int j = 0; j < width; j ++)
{
if(qGray(pSrc[j])>tmp)
pDest[j] = 255;
else
pDest[j] = 0;
}
}
break;
}
image_widget_label->setPixmap(QPixmap::fromImage(ret));
}
void Mark_System::slot_Save()
{
QString filename1 = QFileDialog::getSaveFileName(this,tr("Save Image"),"",tr("Images (*.png *.bmp *.jpg)")); //选择路径
QScreen *screen = QGuiApplication::primaryScreen();
screen->grabWindow(image_widget_label->winId()).save(filename1);
}
没有合适的资源?快使用搜索试试~ 我知道了~
Qt 实现 图片 二值化 灰度化代码
共15个文件
png:3个
jpg:2个
cpp:2个
需积分: 50 50 下载量 125 浏览量
2018-07-18
17:35:35
上传
评论 4
收藏 610KB ZIP 举报
温馨提示
Qt实现图片的二值化,灰度化并保存图片及显示图片。 Qt实现图片的二值化,灰度化并保存图片及显示图片。
资源推荐
资源详情
资源评论
收起资源包目录
Mark_System.zip (15个子文件)
Mark_System
Mark_System
mark_system.cpp 5KB
Mark_System.vcxproj 11KB
mark_system.ui 4KB
main.cpp 171B
Mark_System.vcxproj.user 619B
mark_system.h 725B
Mark_System.vcxproj.filters 3KB
mark_system.qrc 71B
UI
image
love.png 361KB
1.png 144KB
2.png 8KB
z.jpg 38KB
zd.jpg 48KB
Mark_System.suo 22KB
Mark_System.sln 900B
共 15 条
- 1
资源评论
ZzzzzzzzzzPopo
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功