3838import java .awt .image .BufferedImage ;
3939import java .awt .image .DataBufferInt ;
4040import java .nio .ByteBuffer ;
41+ import java .nio .ByteOrder ;
4142import java .nio .IntBuffer ;
4243import java .io .File ;
4344import java .lang .reflect .Field ;
@@ -1067,6 +1068,72 @@ public static void ARGBtoBGRA(Mat rgba, Mat bgra){
10671068 public int getSize (){
10681069 return width * height ;
10691070 }
1071+
1072+
1073+ /**
1074+ * Convert a 3 channel OpenCV Mat object into
1075+ * pixels to be shoved into a 4 channel ARGB PImage's
1076+ * pixel array.
1077+ *
1078+ * @param m
1079+ * A Mat you want converted
1080+ */
1081+ public int [] threeChanMatToARGBPixels (Mat m ){
1082+ int pImageChannels = 4 ;
1083+ int numPixels = m .width ()*m .height ();
1084+ int [] intPixels = new int [numPixels ];
1085+ Mat m2 = new Mat ();
1086+
1087+ // Assumes output PImage is ARGB.
1088+ Imgproc .cvtColor (m , m2 , Imgproc .COLOR_RGB2RGBA );
1089+ byte [] matPixels = new byte [numPixels *pImageChannels ];
1090+
1091+ m2 .get (0 ,0 , matPixels );
1092+ ByteBuffer .wrap (matPixels ).order (ByteOrder .LITTLE_ENDIAN ).asIntBuffer ().get (intPixels );
1093+ return intPixels ;
1094+ }
1095+
1096+ /**
1097+ * Convert a single channel, gray OpenCV Mat object into
1098+ * pixels to be shoved into a 4 channel ARGB PImage's
1099+ * pixel array.
1100+ *
1101+ * @param m
1102+ * A Mat you want converted
1103+ */
1104+ public int [] grayMatToARGBPixels (Mat m ){
1105+ int pImageChannels = 4 ;
1106+ int numPixels = m .width ()*m .height ();
1107+ int [] intPixels = new int [numPixels ];
1108+ Mat m2 = new Mat ();
1109+
1110+ // Assumes output PImage is ARGB.
1111+ Imgproc .cvtColor (m , m2 , Imgproc .COLOR_GRAY2RGBA );
1112+ byte [] matPixels = new byte [numPixels *pImageChannels ];
1113+
1114+ m2 .get (0 ,0 , matPixels );
1115+ ByteBuffer .wrap (matPixels ).order (ByteOrder .LITTLE_ENDIAN ).asIntBuffer ().get (intPixels );
1116+ return intPixels ;
1117+ }
1118+
1119+ /**
1120+ * Convert a 4 channel OpenCV Mat object into
1121+ * pixels to be shoved into a 4 channel ARGB PImage's
1122+ * pixel array.
1123+ *
1124+ * @param m
1125+ * A Mat you want converted
1126+ */
1127+ public int [] fourChanMatToARGBPixels (Mat m ){
1128+ int pImageChannels = 4 ;
1129+ int numPixels = m .width ()*m .height ();
1130+ int [] intPixels = new int [numPixels ];
1131+ byte [] matPixels = new byte [numPixels *pImageChannels ];
1132+
1133+ m .get (0 ,0 , matPixels );
1134+ ByteBuffer .wrap (matPixels ).order (ByteOrder .LITTLE_ENDIAN ).asIntBuffer ().get (intPixels );
1135+ return intPixels ;
1136+ }
10701137
10711138
10721139 /**
@@ -1087,23 +1154,11 @@ public void toPImage(Mat m, PImage img){
10871154 img .loadPixels ();
10881155
10891156 if (m .channels () == 3 ){
1090- byte [] matPixels = new byte [width *height *3 ];
1091- m .get (0 ,0 , matPixels );
1092- for (int i = 0 ; i < m .width ()*m .height ()*3 ; i +=3 ){
1093- img .pixels [PApplet .floor (i /3 )] = parent .color (matPixels [i +2 ]&0xFF , matPixels [i +1 ]&0xFF , matPixels [i ]&0xFF );
1094- }
1157+ img .pixels = threeChanMatToARGBPixels (m );
10951158 } else if (m .channels () == 1 ){
1096- byte [] matPixels = new byte [width *height ];
1097- m .get (0 ,0 , matPixels );
1098- for (int i = 0 ; i < m .width ()*m .height (); i ++){
1099- img .pixels [i ] = parent .color (matPixels [i ]&0xFF );
1100- }
1159+ img .pixels = grayMatToARGBPixels (m );
11011160 } else if (m .channels () == 4 ){
1102- byte [] matPixels = new byte [width *height *4 ];
1103- m .get (0 ,0 , matPixels );
1104- for (int i = 0 ; i < m .width ()*m .height ()*4 ; i +=4 ){
1105- img .pixels [PApplet .floor (i /4 )] = parent .color (matPixels [i +2 ]&0xFF , matPixels [i +1 ]&0xFF , matPixels [i ]&0xFF , matPixels [i +3 ]&0xFF );
1106- }
1161+ img .pixels = fourChanMatToARGBPixels (m );
11071162 }
11081163
11091164 img .updatePixels ();
0 commit comments