Problema
Tienes una matriz 2D. Usó la función Loc para ubicar elementos interesantes, pero Loc devolvió índices para una matriz 1D. ¿Cómo puede volver a convertir los índices 1D en índices 2D?
Solución
Deberá usar las funciones Floor y Mod y deberá saber la cantidad de columnas en su matriz 2D. Aquí están las tres líneas, enterradas en el ejemplo comentado a continuación. pixmat es una matriz 2D, Loc encuentra los píxeles distintos de cero, que son negros:
blacklocs = (Loc(pixmat) - 1); // blacklocs es una matriz de índices 1D (basado en 0)
filas = Floor( (blacklocs) / nc ) + 1; // nc es el número de columnas en el pixmat 2D
collocs = Mod( (blacklocs), nc ) + 1; // rowlocs y collocs son índices 2D (basados en 1)
// we need a bitmap to work with; use a text box with a moderate size font
textbox = Text Box( "Gargoyle", <<SETFONTSIZE> .5 are antialiasing fading to white background of the "paper".
// the next statement makes the 0 to .5 values be 1 and the .5 to 1 values be 0
pixmat = pixmat < .75; // cut off edge at some gray value. flip the 0..1 significance.
// the pixels are in a 2D matrix of rows and cols. get the dimensions.
nr = N Rows( pixmat );
nc = N Cols( pixmat );
// now we can find the location of the black pixels (1s) that form the text.
// the LOC function returns a 1D matrix of the offsets of non-zero
// elements in the 2D matrix pixmat. if pixmat was 3 rows of 5 elements each,
// the LOC could return a 1D matrix of 0 to 15 elements with values from 1 to 15.
// JMP uses 1-based indexing. subtract one to get 0-based for future steps.
blacklocs = (Loc( pixmat ) - 1);
// Here's the JSL that convers the 1D answers back to 2D answers.
// use the 0-based blacklocs (previous statement), then add 1 to
// turn the 0-based results back into the 1-based indexes JMP expects.
rowlocs = Floor( (blacklocs) / nc ) + 1;
collocs = Mod( (blacklocs), nc ) + 1;
// Show the results
scale = 16; // try bigger or smaller scale for your screen size
New Window( "Example",
Graph Box(
// make the graph the same aspect ratio as the bitmap
framesize( nc * scale, nr * scale ),
// make the point marker size big; .65 is approximately right
<<MARKER size=""></MARKER></SETFONTSIZE>
Pixels from a bitmap, displayed as markers in a graphPixels from a bitmap, displayed as markers in a graphPíxeles de un mapa de bits, mostrados como marcadores en un gráfico
Discusión
El -1 y el +1 son importantes, no los dejes fuera. Lea los comentarios basados en 1 y 0 en el JSL.
El texto en el mapa de bits tiene suavizado, lo que significa que los píxeles del borde son tonos de gris proporcionales a cómo habría caído el borde perfecto del carácter entre los píxeles. Elegí un umbral gris de .75 porque me gustó más el resultado que .5; su aplicación probablemente no se ocupará de eso en absoluto.
La función Shape() de JMP convierte la forma de una matriz. Eso no ayudará aquí.
Muchas de las sentencias del ejemplo están operando en una matriz sin utilizar un bucle for JSL explícito. Bueno para la velocidad.
Ver también
https://www.jmp.com/support/help/14-2/matrix-functions.shtml
ver también: ¿Qué es la matriz JSL? para una introducción a las matrices en JMP.