图片素描化

发布时间 2023-11-22 17:22:39作者: wljss

去年暑假无聊搞的小项目

#include <graphics.h>  
#include <conio.h>
#include <stdio.h>
#include<iostream>
using namespace std;
int width,hight;
IMAGE photo1;
const int N=2000,M=2000;//Warning!!!:  change this number to match the picture
COLORREF ys0[N][M],ys1[N][M],ys2[N][M],ys3[N][M],ys4[N][M];
void huoqu()//获取颜色
{
	for(int i=0;i<width;++i)
		for(int j=0;j<hight;++j)
			ys0[i][j]=getpixel(i,j);
}
void quse_fanxiang()//去色+反向
{
	for(int i=0;i<=width;++i)
		for(int j=0;j<hight;++j)
		{
			int r = GetRValue(ys0[i][j]);
			int g = GetGValue(ys0[i][j]);
			int b = GetBValue(ys0[i][j]);
			int minn=min(min(r,g),b),maxx=max(max(r,g),b);
			ys1[i][j]=RGB((minn+maxx)/2,(minn+maxx)/2,(minn+maxx)/2);//去色
		}//(r*299+g*587+b*114+1000)/500
	//FlushBatchDraw();//后期处理


	//return;

	for(int i=0;i<=width;++i)
		for(int j=0;j<hight;++j)//反向
		{
			int r = GetRValue(ys1[i][j]);
			ys2[i][j]=RGB(255-r,255-r,255-r);
		}

}
void lvjing()
{
	int radius=2;
	for(int i=0;i<=width;++i)
		for(int j=0;j<hight;++j)//最小值滤镜
		{
			int minn=114514;
			for(int k=i-radius;k<=i+radius;++k)
				for(int l=j-radius;l<=j+radius;++l)
					if((i-k)*(i-k)+(j-l)*(j-l)<=radius*radius)minn=min(minn,GetRValue(ys2[k][l]));
			ys3[i][j]=RGB(minn,minn,minn);
		}
}
void yansejiandan()//颜色减淡
{
	float H0,S0,L0,H3,S3,L3;
	for(int i=0;i<=width;++i)
		for(int j=0;j<hight;++j)
		{
			RGBtoHSL(ys1[i][j],&H0,&S0,&L0);
			RGBtoHSL(ys3[i][j],&H3,&S3,&L3);
			ys4[i][j]=HSLtoRGB(H0,S0,L0+L3);
		}
}
void hua()
{
	//BeginBatchDraw();
	FlushBatchDraw();//后期处理

	for(int i=0;i<width;++i)
		for(int j=0;j<hight;++j)
			putpixel(i,j,ys4[i][j]);
}
int main()
{
	width=100;hight=100;
	initgraph(width,hight);
	setbkcolor(BLACK);
	cleardevice();

	loadimage(&photo1,_T(".\\img\\mj.jpg"));
	putimage(0,0,&photo1);
	width=photo1.getwidth();hight=photo1.getheight();
	initgraph(width,hight);
	setbkcolor(BLACK);
	cleardevice();
	putimage(0,0,&photo1);

	quse_fanxiang();


	huoqu();
	quse_fanxiang();
	lvjing();
	yansejiandan();
	hua();
	_getch();
	closegraph();
	return 0;
}