WeiFenDib.cpp
Upload User: szkelitina
Upload Date: 2022-05-21
Package Size: 6797k
Code Size: 14k
Category:

Special Effects

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "windowsx.h"
  3. #include "math.h"
  4. #include "WeiFenDib.h"
  5. #include "MainFrm.h"
  6. #include "DynSplitView2.h"
  7. #include "CDIB.h"
  8. WeiFenDib::WeiFenDib()
  9. {
  10. }
  11. WeiFenDib::~WeiFenDib()
  12. {
  13. }
  14. /***************************************************************/           
  15. /*函数名称:ZongXiang()                                        */
  16. /*函数类型:void                                               */
  17. /*返回值:处理成功返回TRUE;处理失败返回FALSE。                 */                                       
  18. /*功能:对图像进行纵向微分                                     */
  19. /***************************************************************/
  20. void WeiFenDib::ZongXiang()
  21. {    
  22.     BYTE  *p_data;     //原图数据区指针
  23. int wide,height;    //原图长、宽
  24. p_data=this->GetData ();
  25.     wide=this->GetWidth ();
  26.     height=this->GetHeight ();
  27. if (m_pBitmapInfoHeader->biBitCount<9) //灰度图像
  28. {     
  29.         LPBYTE   temp=new BYTE [wide*height];    //开辟图像一缓冲区
  30. memset(temp,255,wide*height);
  31. for(int j=1;j<height-1;j++)
  32. for(int i=1;i<wide-1;i++)
  33. //当前像素的灰度值等于其和左方像素灰度值之差的绝对值
  34. temp[wide*j+i]=abs(p_data[wide*j+i]-p_data[wide*j+(i-1)]);
  35. //将缓冲区中的图像复制回原图数据区
  36. memcpy(p_data, temp,wide*height);
  37. //删除缓冲区
  38. delete  temp;
  39. }
  40. else //24位彩色
  41. {    
  42. int DibWidth;    //原图长、宽
  43. DibWidth=this->GetDibWidthBytes();   //取得原图的每行字节数
  44. BYTE *p_temp=new BYTE[height*DibWidth];
  45. for(int j=1;j<height-1;j++) // 每行
  46. {
  47. for(int i=3;i<DibWidth-3;i++) // 每列
  48. {
  49. int pby_pt=0;
  50. pby_pt=*(p_data+(height-j-1)*DibWidth+i)-*(p_data+(height-j-1)*DibWidth+i-3);
  51.                 *(p_temp+(height-j-1)*DibWidth+i)=abs(pby_pt);
  52. }
  53. }
  54. memcpy(p_data,p_temp,height*DibWidth);  // 复制处理后的图像
  55.         delete []p_temp;  //删除暂时分配内存
  56. }    
  57. }
  58. /***************************************************************/           
  59. /*函数名称:HengXiang()                                        */
  60. /*函数类型:void                                               */
  61. /*返回值:处理成功返回TRUE;处理失败返回FALSE。                 */                                       
  62. /*功能:对图像进行横向微分                                     */
  63. /***************************************************************/
  64. void WeiFenDib::HengXiang()
  65. {
  66.     LPBYTE p_data;     //原图数据区指针
  67.     int wide,height;    //原图长、宽
  68. p_data=this->GetData ();
  69.     wide=this->GetWidth ();
  70.     height=this->GetHeight ();
  71. if (m_pBitmapInfoHeader->biBitCount<9) //灰度图像
  72. {     
  73.         LPBYTE  temp=new BYTE [wide*height];    //开辟图像一缓冲区
  74. memset(temp,255,wide*height);
  75. for(int j=1;j<height-1;j++)
  76.     for(int i=1;i<wide-1;i++)
  77. //当前像素的灰度值等于其和上方像素灰度值之差的绝对值
  78. temp[wide*j+i]=abs(p_data[wide*j+i]-p_data[wide*(j-1)+i]);
  79.         //将缓冲区中的图像复制回原图数据区
  80. memcpy(p_data, temp,wide*height);
  81. //删除缓冲区
  82. delete  temp;
  83. }
  84. else  //24位彩色
  85. {    
  86. int DibWidth;    //原图长、宽
  87. DibWidth=this->GetDibWidthBytes();   //取得原图的每行字节数
  88. BYTE *p_temp=new BYTE[height*DibWidth];
  89. for(int j=1;j<height-1;j++) // 每行
  90. {
  91. for(int i=3;i<DibWidth-3;i++) // 每列
  92. {
  93. int pby_pt=0;
  94. pby_pt=*(p_data+(height-j-1)*DibWidth+i)-*(p_data+(height-j)*DibWidth+i);
  95.                 *(p_temp+(height-j-1)*DibWidth+i)=abs(pby_pt);
  96. }
  97. }
  98. memcpy(p_data,p_temp,height*DibWidth);  // 复制处理后的图像
  99.         delete []p_temp;  //删除暂时分配内存
  100. }    
  101. }
  102. /***************************************************************/           
  103. /*函数名称:ShuangXiang()                                      */
  104. /*函数类型:void                                               */
  105. /*返回值:处理成功返回TRUE;处理失败返回FALSE。                 */                                       
  106. /*功能:对图像进行双向一次微分                                 */
  107. /***************************************************************/
  108. void WeiFenDib::ShuangXiang()
  109. {
  110.     LPBYTE  p_data;     //原图数据区指针
  111. int wide,height;    //原图长、宽 
  112. p_data=this->GetData ();
  113.     wide=this->GetWidth ();
  114.     height=this->GetHeight ();
  115. if (m_pBitmapInfoHeader->biBitCount<9) //灰度图像
  116. {     
  117.         LPBYTE temp=new BYTE [wide*height];    //开辟图像一缓冲区
  118. memset(temp,255,wide*height);
  119. for(int j=1;j<height-1;j++)
  120. for(int i=1;i<wide-1;i++)
  121. //根据双向一次微分公式计算当前像素的灰度值
  122. temp[wide*j+i]=(int)sqrt((p_data[wide*j+i]-p_data[wide*j+(i-1)])*(p_data[wide*j+i]-p_data[wide*j+(i-1)])+(p_data[wide*j+i]-p_data[wide*(j-1)+i])*(p_data[wide*j+i]-p_data[wide*(j-1)+i]));
  123.         //将缓冲区中的图像复制回原图数据区
  124. memcpy(p_data, temp,wide*height);
  125. //删除缓冲区
  126. delete  temp;
  127. }
  128. else //24位彩色
  129. {    
  130. int DibWidth;    //原图长、宽
  131. DibWidth=this->GetDibWidthBytes();   //取得原图的每行字节数
  132. BYTE *p_temp=new BYTE[height*DibWidth];
  133. for(int j=1;j<height-1;j++) // 每行
  134. {
  135. for(int i=3;i<DibWidth-3;i++) // 每列
  136. {
  137. int pby_pt1=0,pby_pt2=0;
  138. pby_pt1=*(p_data+(height-j-1)*DibWidth+i)-*(p_data+(height-j-1)*DibWidth+i-3);
  139.                 pby_pt2=*(p_data+(height-j-1)*DibWidth+i)-*(p_data+(height-j)*DibWidth+i);
  140.                 *(p_temp+(height-j-1)*DibWidth+i)=(int)sqrt(pby_pt1*pby_pt1+pby_pt2*pby_pt2);
  141. }
  142. }
  143. memcpy(p_data,p_temp,height*DibWidth);  // 复制处理后的图像
  144.         delete []p_temp;  //删除暂时分配内存
  145. }
  146. }
  147. /***************************************************************/           
  148. /*函数名称:ErCi1()                                            */
  149. /*函数类型:void                                               */
  150. /*返回值:处理成功返回TRUE;处理失败返回FALSE。                 */                                       
  151. /*功能:对图像进行二次微分                                     */
  152. /*算子如下:       +1  -2  +1                                  */
  153. /*                 -2  +4  -2                                  */
  154. /*                 +1  -2  +1                                  */
  155. /*                                                             */
  156. /***************************************************************/
  157. void WeiFenDib::ErCi1()
  158. {
  159.     LPBYTE p_data;     //原图数据区指针
  160. int wide,height;    //原图长、宽
  161. p_data=this->GetData ();
  162.     wide=this->GetWidth ();
  163.     height=this->GetHeight ();
  164. if (m_pBitmapInfoHeader->biBitCount<9) //灰度图像
  165. {     
  166.         LPBYTE   temp=new BYTE [wide*height];    //开辟图像一缓冲区
  167. memset(temp,255,wide*height);   //初始化为255
  168. for(int j=1;j<height-1;j++)
  169.     for(int i=1;i<wide-1;i++)
  170. //根据算子计算每个像素的灰度值
  171. temp[wide*j+i]=abs(p_data[wide*(j-1)+(i-1)]-2*p_data[wide*(j-1)+i]+p_data[wide*(j-1)+(i+1)]-2*p_data[wide*j+(i-1)]+4*p_data[wide*j+i]-2*p_data[wide*j+(i+1)]+p_data[wide*(j+1)+(i-1)]-2*p_data[wide*(j+1)+i]+p_data[wide*(j+1)+(i+1)]);
  172.         //将缓冲区中的图像复制回原图数据区
  173. memcpy(p_data, temp,wide*height);
  174.         //删除缓冲区
  175. delete  temp;
  176. }
  177. else  //24位彩色
  178. {
  179. int DibWidth;    //原图长、宽
  180. DibWidth=this->GetDibWidthBytes();   //取得原图的每行字节数
  181. BYTE *p_temp=new BYTE[height*DibWidth];
  182. for(int j=1;j<height-2;j++) // 每行
  183. {
  184. for(int i=3;i<DibWidth-8;i++) // 每列
  185. {
  186. int pby_pt=0;
  187. pby_pt=*(p_data+(height-j)*DibWidth+i-3)-2*(*(p_data+(height-j)*DibWidth+i))
  188. +*(p_data+(height-j)*DibWidth+i+3)-2*(*(p_data+(height-j-1)*DibWidth+i-3))
  189. +4*(*(p_data+(height-j-1)*DibWidth+i))-2*(*(p_data+(height-j-1)*DibWidth+i+3))
  190. +*(p_data+(height-j-2)*DibWidth+i-3)-2*(*(p_data+(height-j-2)*DibWidth+i))
  191. +*(p_data+(height-j-2)*DibWidth+i+3);
  192.                 *(p_temp+(height-j-1)*DibWidth+i)=abs(pby_pt);
  193. }
  194.     }
  195. memcpy(p_data,p_temp,height*DibWidth);  // 复制处理后的图像
  196. delete []p_temp;  //删除暂时分配内存
  197. }
  198. }
  199. /***************************************************************/           
  200. /*函数名称:ErCi2()                                            */
  201. /*函数类型:void                                               */
  202. /*返回值:处理成功返回TRUE;处理失败返回FALSE。                 */                                       
  203. /*功能:对图像进行二次微分                                     */
  204. /*算子如下:    +1  +1  +1                                     */
  205. /*              +1  -8  +1                                     */
  206. /*              +1  +1  +1                                     */
  207. /*                                                             */
  208. /***************************************************************/
  209. void WeiFenDib::ErCi2()
  210. {
  211.     LPBYTE  p_data;     //原图数据区指针
  212. int wide,height;    //原图长、宽
  213. p_data=this->GetData ();
  214.     wide=this->GetWidth ();
  215.     height=this->GetHeight ();
  216. if (m_pBitmapInfoHeader->biBitCount<9) //灰度图像
  217. {     
  218.         LPBYTE  temp=new BYTE [wide*height];    //开辟图像一缓冲区
  219. memset(temp,255,wide*height);
  220. for(int j=1;j<height-1;j++)
  221. for(int i=1;i<wide-1;i++)
  222. temp[wide*j+i]=abs(p_data[wide*(j-1)+(i-1)]+p_data[wide*(j-1)+i]+p_data[wide*(j-1)+(i+1)]+p_data[wide*j+(i-1)]-8*p_data[wide*j+i]+p_data[wide*j+(i+1)]+p_data[wide*(j+1)+(i-1)]+p_data[wide*(j+1)+i]+p_data[wide*(j+1)+(i+1)]);
  223.         memcpy(p_data, temp,wide*height);
  224. delete  temp;
  225. }
  226. else //24位彩色
  227. {    
  228. int DibWidth;    //原图长、宽
  229. DibWidth=this->GetDibWidthBytes();   //取得原图的每行字节数
  230. BYTE *p_temp=new BYTE[height*DibWidth];
  231. for(int j=1;j<height-2;j++) // 每行
  232. {
  233. for(int i=3;i<DibWidth-8;i++) // 每列
  234. {
  235. int pby_pt=0;
  236. pby_pt=*(p_data+(height-j)*DibWidth+i-3)+*(p_data+(height-j)*DibWidth+i)
  237. +*(p_data+(height-j)*DibWidth+i+3)+*(p_data+(height-j-1)*DibWidth+i-3)
  238. -8*(*(p_data+(height-j-1)*DibWidth+i))+*(p_data+(height-j-1)*DibWidth+i+3)
  239. +*(p_data+(height-j-2)*DibWidth+i-3)+*(p_data+(height-j-2)*DibWidth+i)
  240. +*(p_data+(height-j-2)*DibWidth+i+3);
  241.                 *(p_temp+(height-j-1)*DibWidth+i)=abs(pby_pt);
  242. }
  243. }
  244. memcpy(p_data,p_temp,height*DibWidth);  // 复制处理后的图像
  245.         delete []p_temp;  //删除暂时分配内存
  246. }
  247. }
  248. /***************************************************************/           
  249. /*函数名称:ErCi3()                                            */
  250. /*函数类型:void                                               */
  251. /*返回值:处理成功返回TRUE;处理失败返回FALSE。                 */                                       
  252. /*功能:对图像进行二次微分                                     */
  253. /*算子如下:    +1+1+1                                         */
  254. /*              +1+1+1                                         */
  255. /*              +1+1+1                                         */
  256. /*        +1+1+1-4-4-4 +1+1+1                                  */
  257. /*        +1+1+1-4-4-4 +1+1+1                                  */
  258. /*        +1+1+1-4-4-4 +1+1+1                                  */
  259. /*              +1+1+1                                         */
  260. /*              +1+1+1                                         */
  261. /*              +1+1+1                                         */
  262. /*                                                             */
  263. /***************************************************************/
  264. void WeiFenDib::ErCi3()
  265. {
  266.     LPBYTE  p_data;     //原图数据区指针
  267. int wide,height;//原图长、宽
  268.     p_data=this->GetData ();
  269.     wide=this->GetWidth ();
  270.     height=this->GetHeight ();
  271. if (m_pBitmapInfoHeader->biBitCount<9) //灰度图像
  272. {
  273.     int x,y;   //循环变量
  274.         int cent3x3,up3x3,down3x3,right3x3,left3x3;   //中心的上下左右四个区域积的和
  275.         BYTE**  temp1=new BYTE*  [height+8];
  276.     BYTE** temp2=new BYTE*  [height];
  277.     for (y=0;y<height;y++)
  278.     temp2[y]=new BYTE [wide];
  279.     for (y=0;y<height+8;y++)
  280.      temp1[y]=new BYTE [wide+8];
  281.     for (y=0;y<height;y++)
  282.     for(x=0;x<wide;x++)
  283.     temp2[y][x]=0;
  284.     for (y=0;y<height+8;y++)
  285.      for(x=0;x<wide+8;x++)
  286.      temp1[y][x]=0;
  287. for (y=0;y<height;y++)
  288.     for(x=0;x<wide;x++)
  289.     temp1[y+4][x+4]=p_data[wide*y+x];
  290. for (y=4;y<height+4;y++)
  291. for(x=4;x<wide+4;x++)
  292. {
  293. cent3x3=-4* temp1[y-1][x-1]-4* temp1[y-1][x]-4* temp1[y-1][x+1]-4* temp1[y][x-1]-4* temp1[y][x]-4* temp1[y][x+1]-4* temp1[y+1][x-1]-4* temp1[y+1][x]-4* temp1[y+1][x+1];
  294. up3x3= temp1[y-4][x-1]+ temp1[y-4][x]+ temp1[y-4][x+1]+ temp1[y-3][x-1]+ temp1[y-3][x]+ temp1[y-3][x+1]+ temp1[y-2][x-1]+ temp1[y-2][x]+ temp1[y-2][x+1];
  295. down3x3= temp1[y+2][x-1]+ temp1[y+2][x]+ temp1[y+2][x+1]+ temp1[y+3][x-1]+ temp1[y+3][x]+ temp1[y+3][x+1]+ temp1[y+4][x-1]+ temp1[y+4][x]+ temp1[y+4][x+1];
  296. right3x3=temp1[y-1][x+2]+ temp1[y-1][x+3]+ temp1[y-1][x+4]+ temp1[y][x+2]+ temp1[y][x+3]+ temp1[y][x+4]+ temp1[y+1][x+2]+ temp1[y+1][x+3]+ temp1[y+1][x+4];
  297. left3x3=temp1[y-1][x-4]+ temp1[y-1][x-3]+ temp1[y-1][x-2]+ temp1[y][x-4]+ temp1[y][x-3]+ temp1[y][x-2]+ temp1[y+1][x-4]+ temp1[y+1][x-3]+ temp1[y+1][x-2];
  298. temp2[y-4][x-4]=abs(cent3x3+up3x3+down3x3+right3x3+left3x3);
  299. }
  300. for (y=0;y<height;y++)
  301.  for(x=0;x<wide;x++)
  302.     p_data[wide*y+x]=temp2[y][x];
  303. }
  304. else  //24位彩色
  305. {
  306. int DibWidth;    //原图长、宽
  307. DibWidth=this->GetDibWidthBytes();   //取得原图的每行字节数
  308. BYTE *p_temp=new BYTE[height*DibWidth];
  309. for(int j=1;j<height-2;j++) // 每行
  310. {
  311. for(int i=3;i<DibWidth-8;i++) // 每列
  312. {
  313. int pby_pt=0;
  314. pby_pt=*(p_data+(height-j)*DibWidth+i-3)-2*(*(p_data+(height-j)*DibWidth+i))
  315. +*(p_data+(height-j)*DibWidth+i+3)-2*(*(p_data+(height-j-1)*DibWidth+i-3))
  316. +4*(*(p_data+(height-j-1)*DibWidth+i))-2*(*(p_data+(height-j-1)*DibWidth+i+3))
  317. +*(p_data+(height-j-2)*DibWidth+i-3)-2*(*(p_data+(height-j-2)*DibWidth+i))
  318. +*(p_data+(height-j-2)*DibWidth+i+3);
  319.                 *(p_temp+(height-j-1)*DibWidth+i)=abs(pby_pt);
  320. }
  321. }
  322. memcpy(p_data,p_temp,height*DibWidth);  // 复制处理后的图像
  323. delete []p_temp;  //删除暂时分配内存
  324. BYTE *p_temp1=new BYTE[height*DibWidth];
  325. for(int a=1;a<height-2;a++) // 每行
  326. {
  327. for(int b=3;b<DibWidth-8;b++) // 每列
  328. {
  329. int pby_pt=0;
  330. pby_pt=*(p_data+(height-a)*DibWidth+b-3)+*(p_data+(height-a)*DibWidth+b)
  331. +*(p_data+(height-a)*DibWidth+b+3)+*(p_data+(height-a-1)*DibWidth+b-3)
  332. -8*(*(p_data+(height-a-1)*DibWidth+b))+*(p_data+(height-a-1)*DibWidth+b+3)
  333. +*(p_data+(height-a-2)*DibWidth+b-3)+*(p_data+(height-a-2)*DibWidth+b)
  334. +*(p_data+(height-a-2)*DibWidth+b+3);
  335.                 *(p_temp1+(height-a-1)*DibWidth+b)=abs(pby_pt);
  336. }
  337. }
  338. memcpy(p_data,p_temp,height*DibWidth);  // 复制处理后的图像
  339. delete []p_temp1;  //删除暂时分配内存
  340. }
  341. }