I am trying to get subimages with Opencv in iphone.What is the problem in this code?
error from console is: OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /Users/macmade/Desktop/OpenCV-iPhone/OpenCV/build/iPhoneSimulator/../.././tmp/OpenCV-2.0.0/src/cxcore/cxarray.cpp, line 2470
terminate called after throwing an instance of 'cv::Exception'
std::vector<IplImage*> vec_images;
int h = bw->height;
for (int i=0; i<Xleft.size(); ++i) {
CvRect rect = cvRect(Xleft[i], 0, avgx, h);
/* dst image */
IplImage* subimg;
/* copy ROI to subimg */
cvSetImageROI(bw, rect);
cvCopy(bw, subimg, NULL);
vec_images.push_back(subimg);
cvResetImageROI(bw);
}
for (int i=0; vec_images.size(); ++i) {
cvReleaseImage(&vec_images[i]);
}
You must initialize subimg before calling cvCopy.
Related
I am trying to make a S-Function (written in C and using SDL library) in Simulink to read joystick values from the device and output them to Simulink. I used before the standard aerospace library joystick block, however, this does not support compiling (which is needed for Rapid Acceleration). Hence, I decided to write my own S-Function.
I managed to make a simple C program that uses SDL (I downloaded https://www.libsdl.org/download-2.0.php) to read values of joystick and print to terminal:
#include <stdio.h>
#include <unistd.h>
#include "SDL2/SDL.h"
static SDL_Joystick *joy = NULL;
int
main(int argc, char *argv[])
{
// ITIALIZE:
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
// Check for joystick
if (SDL_NumJoysticks() > 0) {
// Open joystick
joy = SDL_JoystickOpen(0);
if (joy) {
printf("Opened Joystick 0\n");
printf("Name: %s\n", SDL_JoystickNameForIndex(0));
printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joy));
printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joy));
printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joy));
} else {
printf("Could not open Joystick 0\n");
return 0;
}
}
SDL_JoystickEventState(SDL_IGNORE);
// READ VALUES:
for (int i = 1; i < 100; ++i){
SDL_JoystickUpdate();
for (int j = 0; j < SDL_JoystickNumAxes(joy); ++j) {
int value = (((int) SDL_JoystickGetAxis(joy, j)) + 32768);
printf("Axes %d: %d ", j, value);
}
printf("\n");
usleep(100000);
}
// CLOSE:
if (SDL_JoystickGetAttached(joy)) {
SDL_JoystickClose(joy);
}
return 1;
}
I compile the C code in Code::Blocks with linkers:
-lmingw32 -lSDL2main -lSDL2
The compiled .exe requires runtime binary SDL.dll, which is simply located in the same folder as the compilation, and everything works.
The problem is, how to transfer the above to work in Simulink environment? I have transferred the code into Simulink S-Function builder:
/* Includes_BEGIN */
#include "SDL2/SDL.h"
#include <stdio.h>
/* Includes_END */
/* Externs_BEGIN */
static SDL_Joystick *joy = NULL;
/* Externs_END */
void sunf_joystick_Start_wrapper(void)
{
/* Start_BEGIN */
// ITIALIZE:
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
// Check for joystick
if (SDL_NumJoysticks() > 0) {
// Open joystick
joy = SDL_JoystickOpen(0);
if (joy) {
ssPrintf("Opened Joystick 0\n");
ssPrintf("Name: %s\n", SDL_JoystickNameForIndex(0));
ssPrintf("Number of Axes: %d\n", SDL_JoystickNumAxes(joy));
ssPrintf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joy));
ssPrintf("Number of Balls: %d\n", SDL_JoystickNumBalls(joy));
} else {
ssWarning("Warning:Joystick","Could not open Joystick 0\n");
}
}
SDL_JoystickEventState(SDL_IGNORE);
/* Start_END */
}
void sunf_joystick_Outputs_wrapper(real_T *y0)
{
/* Output_BEGIN */
SDL_JoystickUpdate();
for (int j = 0; j < SDL_JoystickNumAxes(joy); ++j) {
y0[j] = (((int) SDL_JoystickGetAxis(joy, j)) + 32768);
}
/* Output_END */
}
void sunf_joystick_Terminate_wrapper(void)
{
/* Terminate_BEGIN */
if (SDL_JoystickGetAttached(joy)) {
SDL_JoystickClose(joy);
}
/* Terminate_END */
}
and added the LIB_PATH and INCL_PATH to point for the SDL library:
S-function builder Libraries tab
However, I get a lot of similar error messages when trying to build through the GUI:
C:\Users\user\AppData\Local\Temp\mex_121831936796334_22096\sunf_joystick_wrapper.obj:sunf_joystick_wrapper.c:(.text+0xe): undefined reference to `SDL_InitSubSystem'
To me it seems that the libraries are not linked correctly. An idea how to fix this? I have tried to build it also with mex through MATLAB command line, not successful, and feels also wrong way to do it.
Also, any advice where the the runtime library SDL.dll should be stored or referenced if the compilation is successful?
All files in: https://github.com/JohannesSoikkeli/Simulink_joystick
Many thanks!
I am making a 2d(25x20) grid of sprites. But somehow sprites are getting re positioned by itself.enter image description here
makeLandBlocksMatrix : function () {
this.LAND_BLOCK_TAG = 1;
var blockCounter = 0;
var prices = MMMapData.getPrices();
this._blocks = MMUtility.createArray(MMConstants.totalNoRowsPerMap,MMConstants.totalNoColsPerMap);
for (var i = 0; i< MMConstants.totalNoRowsPerMap; i++){
for (var j = 0; j< MMConstants.totalNoColsPerMap; j++){
var block = new MMLandBlockSprite();
block.initWithData(res.BlockBlack,prices[blockCounter],this.LAND_BLOCK_TAG);
block.setPosition(cc.p(block.getContentSize().width*0.5 + i * block.getContentSize().width * 1.0, (this._size.height - block.getContentSize().height*0.5) - j * block.getContentSize().height * 1.0));
this.addChild(block);
block.setBg();
block._bg.setOpacity(0.0);
block.setPriceLabel();
block._priceLabel.setOpacity(0.0);
this._blocks[i][j] = block;
this.LAND_BLOCK_TAG++;
blockCounter++;
}
}
}
And same code is working fine with cocos2d-x(c++).
Thanks.
After lots of tweaks and googling, i just used lower version of cocos2d-html(version 3.7). And same code work as expected.There might be problem in rendering pipeline in latest cocos2d-html. And same issue persist in case we try to make Grid of UI component or Basic components(Sprite,Label) as components number increases positioning difference increases(i.e as show in reference image).
I'm capturing images from camera, and I have two function for saving 16-bit(!) image one in PNG and one in TIFF formats.
Could you please explain why the PNG is a very noisy image? like this:
PNG function:
bool save_image_png(const char *file_name,const mono16bit& img)
{
[...]
/* write header */
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[write_png_file] Error during writing header");
png_set_IHDR(png_ptr, info_ptr, width, height,
bit_depth,PNG_COLOR_TYPE_GRAY , PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
/* write bytes */
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[write_png_file] Error during writing bytes");
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
for (y=0; y<height; y++)
row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));
for (y = 0; y < height; y++)
{
row_pointers[y] = (png_bytep)img.getBuffer() + y * width*2;
}
png_write_image(png_ptr, row_pointers);
/* end write */
[...]
}
and TIFF function:
bool save_image(const char *fname,const mono16bit& img)
{
[...]
for(y=0; y<height; y++) {
if((err=TIFFWriteScanline(tif,(tdata_t)(img.getBuffer()+width*y),y,0))==-1)
break;
}
TIFFClose(tif);
if(err==-1) {
fprintf(stderr,"Error writing to %s file\n",fname);
return false;
}
return true;
//#endif //USE_LIBTIFF
}
Thank you!
png_set_swap does nothing. You have to actually flip bytes in each pixel of the image.
If you’re on a PC and have SSSE3 or newer, a good way is _mm_shuffle_epi8 instruction, make a permute vector with _mm_setr_epi8.
If you’re on ARM and have NEON, use vrev16q_u8 instruction instead.
Perhaps you have a byte-order problem.
Try adding:
png_set_swap(png_ptr);
before saving the image
How does the cpSpaceShapeQuery function work? I can not find any doc about it.
Andrea
Yeah... I never got around to documenting that... Sorry. Basically you create a body and shape (neither needs to be added to the space) and use that to query much like the other query functions.
This code snippet makes a copy of a body and shape on the stack and then simulates it out to it's first predicted collision point drawing the path as it goes.
cpBody body = *(originalBody);
cpPolyShape shape = *((cpPolyShape *)originalShape);
shape.shape.body = &body;
cpFloat dt = 1.0f/60.0f;
cpVect gravity = space->gravity;
int count = 0;
for(int i=0; i<300; i++){
cpBodyUpdatePosition(&body, dt);
cpBodyUpdateVelocity(&body, gravity, 1.0f, dt);
if(cpSpaceShapeQuery(space, (cpShape *)&shape, NULL, NULL)){
quads[count%maxQuads] = quad(body.p, body.rot, CGRectMake(0, 2*32, 64, 64), tsize);
count++;
break;
}
if(i%10==0){
quads[count%maxQuads] = quad(body.p, body.rot, rect, tsize);
count++;
}
}
Ok, so what i am doing is adding a new Overlay to an existing DICOM file & saving it(The DICOM file now has two overlays). Everything saves without errors & both DICOM viewers Sante & ClearCanvas-Workstation open the file, but only Sante displays both overlays.
Now when I look at the tags within the DICOM file, the OverlayData(6000) 'VR' is 'OW' & the OverlayData(6002) 'VR' is 'OB'.
So my problem is how to create a new Tag with a 'VR' of 'OW' because that is the correct one to use for OverlayData.
Here is the code i'm using to add the new Overlay to the DicomFile.DataSet::
NOTE, after I create the overlay I do write visible pixel data into it.
void AddOverlay()
{
int newOverlayIndex = 0;
for(int i = 0; i != 16; ++i)
{
if(!DicomFile.DataSet.Contains(GetOverlayTag(i, 0x3000)))
{
newOverlayIndex = i;
break;
}
}
//Columns
uint columnsTag = GetOverlayTag(newOverlayIndex, 0x0011);
DicomFile.DataSet[columnsTag].SetUInt16(0, (ushort)CurrentData.Width);
//Rows
uint rowTag = GetOverlayTag(newOverlayIndex, 0x0010);
DicomFile.DataSet[rowTag].SetUInt16(0, (ushort)CurrentData.Height);
//Type
uint typeTag = GetOverlayTag(newOverlayIndex, 0x0040);
DicomFile.DataSet[typeTag].SetString(0, "G");
//Origin
uint originTag = GetOverlayTag(newOverlayIndex, 0x0050);
DicomFile.DataSet[originTag].SetUInt16(0, 1);
DicomFile.DataSet[originTag].SetUInt16(1, 1);
//Bits Allocted
uint bitsAllocatedTag = GetOverlayTag(newOverlayIndex, 0x0100);
DicomFile.DataSet[bitsAllocatedTag].SetUInt16(0, 1);
//Bit Position
uint bitPositionTag = GetOverlayTag(newOverlayIndex, 0x0100);
DicomFile.DataSet[bitPositionTag].SetUInt16(0, 0);
//Data
uint dataTag = GetOverlayTag(newOverlayIndex, 0x3000);
DicomFile.DataSet[dataTag].SetNullValue();//<<< Needs to be something else
byte[] bits = new byte[(CurrentData.Width*CurrentData.Height)/8];
for(int i = 0; i != bits.Length; ++i) bits[i] = 0;
DicomFile.DataSet[dataTag].Values = bits;
}
public static uint GetOverlayTag(int overlayIndex, short element)
{
short group = (short)(0x6000 + (overlayIndex*2));
byte[] groupBits = BitConverter.GetBytes(group);
byte[] elementBtis = BitConverter.GetBytes(element);
return BitConverter.ToUInt32(new byte[]{elementBtis[0], elementBtis[1], groupBits[0], groupBits[1]}, 0);
}
So it would seem to me there would be some method like 'DicomFile.DataSet[dataTag].SetNullValue();' to create the tag with a 'VR' of 'OW'. Or maybe theres a totally different way to add an overlay in ClearCanvas idk...
Ok, my confusion was accually caused by a bug in my program.
I was trying to create the "Bit Position" tag by using element "0x0100" instead of "0x0102".
OW vs OB is irrelevant.
Sorry about that...