cocos2dx-3.10 covertToWorldSpace result is wrong when using OrbitCamera - cocos2d-x

cocos2dx-3.10 covertToWorldSpace result is wrong when using OrbitCamera
devices test on:Windows
Steps to Reproduce:
local size = cc.size(400, 400)
local layer = cc.LayerColor:create(cc.c4b(200, 0, 0, 255), size.width, size.height)
layer:setPosition(display.cx - size.width, display.cy - size.height)
self:addChild(layer)
local label = cc.Label:create()
label:setSystemFontSize(30)
label:setSystemFontName("Arial")
label:setString("XXX")
label:setColor(cc.c3b(0, 255, 100))
label:setPosition(size.width * 0.5, 0)
layer:addChild(label)
local lsize = label:getContentSize()
local action = cc.OrbitCamera:create(1, 1, 0, 0, 70, -90, 0)
local callFunc = cc.CallFunc:create(function()
local worldPos = label:convertToWorldSpace(cc.p(lsize.width * 0.5, lsize.height * 0.5))
label = cc.Label:create()
label:setSystemFontSize(30)
label:setSystemFontName("Arial")
label:setString("AAA")
label:setColor(cc.c3b(100, 0, 100))
label:setPosition(worldPos)
self:addChild(label)
end)
layer:runAction(cc.Sequence:create(action, callFunc))

I found a solution:
local mat4 = label:getNodeToParentTransform(self)
local vec4 = {x = lsize.width * 0.5, y = lsize.height * 0.5, z = 0, w = 1}
-- convert to world space (we need to calculate its Z-value)
local pos = mat4_transformVector(mat4, vec4, cc.mat4.createIdentity())
-- convert GL-screen-space coordinates
local worldPos = cc.Camera:getDefaultCamera():projectGL(pos)
enter image description here

Related

Iterate_level after SetRectangle in tesseract Python wrapper

I have an image with few lines of japanese Kanji for which I am trying modifying the boxes boundes before to iterate over and get the font and its attributes.
I could check that Pyplot is displaying the boxes correctly. Also the text is correctly returned if I remove the loop which setRectangle.
My function is as below:
def font_attr(resized):
img = resized
image = Image.fromarray(img)
with PyTessBaseAPI(path='C:\\Users\\mdelal001\\fast_format_assist\\tessdata\\', lang='jpn+msp+hgp', oem=0, psm=3) as api:
api.SetImage(image)
boxes = api.GetComponentImages(RIL.TEXTLINE, True)
delta = 5
image_array = np.array(image)
for box in boxes:
print(box)
box = box[1]
x, y, w, h = box['x'] - delta, box['y'] - delta, box['w'] + 2 * delta, box['h'] + 2 * delta
cv2.line(image_array, (x, y), (x + w, y), (0, 0, 0), 2)
cv2.line(image_array, (x, y), (x, y + h), (0, 0, 0), 2)
cv2.line(image_array, (x + w, y), (x + w, y + h), (0, 0, 0), 2)
cv2.line(image_array, (x, y + h), (x + w, y + h), (0, 0, 0), 2)
plt.imshow(image_array)
plt.show()
for i, (im, box, _, _) in enumerate(boxes):
print(i, (im, box, _, _))
api.SetRectangle(box['x'] - delta, box['y'] - delta, box['w'] + 2 * delta, box['h'] + 2 * delta)
api.Recognize()
ri = api.GetIterator()
font = []
attributes = []
for r in iterate_level(ri, RIL.BLOCK):
symbol = r.GetUTF8Text(RIL.BLOCK)
conf = r.Confidence(RIL.BLOCK)
symbol = symbol.replace('\n',' ').replace(' ', '')
word_attributes = r.WordFontAttributes()
if not symbol:
continue
else:
font.append([symbol, 'confidence: ',conf])
attributes.append(word_attributes)
return font, attributes
The error:
Traceback (most recent call last):
File "c:\Users\m1\fast_format_assist\font_reader.py", line 338, in <module>
attr = font_attr(resized)
File "c:\Users\m1\fast_format_assist\font_reader.py", line 216, in font_attr
symbol = r.GetUTF8Text(RIL.TEXTLINE)
File "tesserocr.pyx", line 820, in tesserocr._tesserocr.PyLTRResultIterator.GetUTF8Text
RuntimeError: No text returned
Better you go with easyocr for this case.
import easyocr
reader = easyocr.Reader(['ch_sim'],gpu=False)
result = reader.readtext('5.png')
for detection in result:
print(detection)
the result is,
([[0, 0], [384, 0], [384, 70], [0, 70]], '二九办一番目', 0.04030529339493764)
([[0, 75], [381, 75], [381, 155], [0, 155]], '二九加二番目', 0.06939456423064959)
([[0, 164], [380, 164], [380, 242], [0, 242]], '二九|4别0去', 0.02179895938530989)
([[0, 249], [385, 249], [385, 335], [0, 335]], '二九|寸危害学', 0.01723975047661578)
([[0, 335], [385, 335], [385, 421], [0, 421]], '二九(丈著盥目', 0.004155675980245961)
([[0, 424], [382, 424], [382, 499], [0, 499]], '二札|击六番目', 0.022589517592635434)
You can install easyocr by pip install easyocr

Color matrix to change dark color

I try to make a function to colorize image - desaturate and replace dark color with supplied RGB color.
I have found similar function from Mario Klingemann AS3 ColorMatrix library, which replace light color while preserving dark.
public function colorize(rgb:int, amount:Number=1):void
{
const LUMA_R:Number = 0.212671;
const LUMA_G:Number = 0.71516;
const LUMA_B:Number = 0.072169;
var r:Number;
var g:Number;
var b:Number;
var inv_amount:Number;
r = (((rgb >> 16) & 0xFF) / 0xFF);
g = (((rgb >> 8) & 0xFF) / 0xFF);
b = ((rgb & 0xFF) / 0xFF);
inv_amount = (1 - amount);
concat([(inv_amount + ((amount * r) * LUMA_R)), ((amount * r) * LUMA_G), ((amount * r) * LUMA_B), 0, 0,
((amount * g) * LUMA_R), (inv_amount + ((amount * g) * LUMA_G)), ((amount * g) * LUMA_B), 0, 0,
((amount * b) * LUMA_R), ((amount * b) * LUMA_G), (inv_amount + ((amount * b) * LUMA_B)), 0, 0,
0, 0, 0, 1, 0]);
}
I tried to modify the matrix but with no success. Please could you provide me any help or link to information or code that make me move forward a bit.
You could try to use the method above, but before you apply it you invert the image and the color. Then after applying the matrix, you invert the image again.
After all I found the trick in this result:
concat([(LUMA_R), (LUMA_G), (LUMA_B), 0, amount*255*r,
(LUMA_R), (LUMA_G), (LUMA_B), 0, amount*255*g,
(LUMA_R), (LUMA_G), (LUMA_B), 0, amount*255*b,
0, 0, 0, 1, 0]);
It does exactly what I needed.
Thanks to Quasimondo for his great AS3 library. Helped me a lot in understanding the color matrix transformations.

How to make a few transitions one after another

I'm currently trying to make a few transitions one after another but I can figure out a way to get many to work in the same function one after each other. My current way (Below) works but I hate how it's so messy.
function screenLowering()
local start = transition.to(backdrop,{time = 1000, x = centerX, y = height/2, onComplete = screenBounce})
end
function screenBounce()
local bounce = transition.to(backdrop,{time = 250, x = centerX, y = backdrop.y - 50, onComplete = screenBounce2})
end
function screenBounce2()
local bounce = transition.to(backdrop,{time = 300, x = centerX, y = backdrop.y + 50})
end
function chain_of_transitions(object, params, ...)
if params then
if params.dx then
params.x, params.dx = object.x + params.dx
end
if params.dy then
params.y, params.dy = object.y + params.dy
end
function params.onComplete()
chain_of_transitions(object, ...)
end
transition.to(backdrop, params)
end
end
chain_of_transitions(backdrop,
{time = 1000, x = centerX, y = height/2},
{time = 250, x = centerX, dy = -50},
{time = 300, x = centerX, dy = 50}
)

Rotation without distortion using Matrix3D

How to assign a value to Matrix3D rotation about the axis Z. (li "rotation")?
The position and size are calculated correctly, but after the addition of rotation, the image is stretched.
I tried two method to rotation:
_positionMatrix.identity();
var v3:Vector.<Vector3D> = new Vector.<Vector3D>(3);
v3 = _positionMatrix.decompose();
v3[0].incrementBy(new Vector3D( x, y, 0 ));// x, y, z
v3[1].incrementBy(new Vector3D(0,0,-rotation*Math.PI/180)); // rotationX, rotationY, rotationZ
v3[2].incrementBy(new Vector3D(width,height,0)); // scaleX, scaleY, scaleZ
_positionMatrix.recompose(v3);
And:
_positionMatrix.identity();
_positionMatrix.appendScale(width,height,1);
_positionMatrix.appendTranslation(x,y,0);
_positionMatrix.appendRotation(-rotation,Vector3D.Z_AXIS );
But the effect is identical:
I find that "PerspectiveProjection" class can help, but cant to understand how to use it with rotation about the axis Z.
For start, if you have such a problem - try to eliminate matrix transforms one by one until you find which matrix is a "bad" one. For example, what will happen if you store only one transformation - scale or translation or rotation?
In your example I can't see any criminal, so the problem is further in code. I can assume that your projection matrix is wrong. Can you provide it? How do you apply final matrix to an object?
Thanks, its true, my perspective Projection matrix was "bad".
This code works perfectly:
private var _vertexData:Vector.<Number> = new <Number>[
0,0,0, 0, 0, 0, 1,// x, y, z, r, g, b,a
1, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 0, 1,
0, 1, 0,0, 0, 0,1
];
var perspectiveProjectionMatrix:Matrix3D = new Matrix3D();
var scaleX:Number = 2.0 / _stage.stageWidth; // 2.0 / 500
var scaleY:Number = -2.0 / _stage.stageHeight; // -2.0 / 375
perspectiveProjectionMatrix.copyRawDataFrom(
new <Number>[
scaleX, 0.0, 0.0, 0.0,
0.0, scaleY, 0.0, 0.0,
0.0, 0.0, -1.0, 0.0,
-1.0, 1.0, 0.0, 1.0
]
);
var modelViewMatrix:Matrix3D = trs(stage.stageWidth/2,stage.stageHeight/2, _rotation*Math.PI/180, _width*_globalScaleX, _height*_globalScaleY );
var resultMatrix:Matrix3D = new Matrix3D();
resultMatrix.prepend(perspectiveProjectionMatrix);
resultMatrix.prepend(modelViewMatrix);
_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0,resultMatrix , true);
Where "trs" is:
public function trs( tx:Number, ty:Number, rotation:Number, xScale:Number, yScale:Number ):Matrix3D {
var data:Vector.<Number> = new <Number>[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
var sin:Number = Math.sin( rotation );
var cos:Number = Math.cos( rotation );
data[0] = cos * xScale;
data[1] = sin * xScale;
data[4] = -sin * yScale;
data[5] = cos * yScale;
data[12] = tx;
data[13] = ty;
var matrix:Matrix3D = new Matrix3D();
matrix.copyRawDataFrom(data);
return matrix;
}

Adding contrast and brightness to image in flex

How can i implement brightness and contrast changes for image in flex
I need to develop a tool for adding brightness or contrast and reducing it
Use ColorMatrixFilter and assigned below matrix to its matrix property
var contrastAdj:ColorMatrixFilter = ColorMatrixFilter(filters.getItemAt(0));
contrastAdj.matrix = getContrastMatrix(value);
private static function getContrastMatrix(value:Number):Array
{
value /= 100;
var s: Number = value + 1;
var o : Number = 128 * (1 - s);
var m:Array = new Array();
m = m.concat([s, 0, 0, 0, o]); // red
m = m.concat([0, s, 0, 0, o]); // green
m = m.concat([0, 0, s, 0, o]); // blue
m = m.concat([0, 0, 0, 1, 0]); // alpha
return m;
}
for brightness matrix use this function
private static function getBrightnessMatrix(value:Number):Array
{
var m:Array = new Array();
m = m.concat([1, 0, 0, 0, value]); // red
m = m.concat([0, 1, 0, 0, value]); // green
m = m.concat([0, 0, 1, 0, value]); // blue
m = m.concat([0, 0, 0, 1, 0]); // alpha
return m;
}
pass values between -100 to 100 to functions.
You might use flash.geom.ColorTransform against an image, at least for previewing. If you need to have your image's pixels changed, I'd say use a Pixel Bender shader that will do what you need. Note however, you will need a backup copy should you desire to change the parameters of that shader.