This will convert leading spaces (even interspersed with tabs) into tabs. Specify the number of spaces to convert by setting the variable. Stray spaces will be collapsed to nothing. Spaces and tabs that appear after any character other than space or tab will not be touched.
tstop=2
sed "s/^\([[:blank:]]*\)\(.*\)/\1\n\2/;h;s/[^[\n]*//;x;s/\n.*//;s/ \{$tstop\}/X/g;s/ //g;G;s/\n//g" inputfile
Example:
[space][space][tab][tab][space][space][space][tab][space]TEXT[space][space][space]
will be converted to
[tab][tab][tab][tab][tab]TEXT[space][space][space]
If that's not exactly what you're looking for, adjustments can be made.